Hacker Newsnew | past | comments | ask | show | jobs | submit | more phpnode's commentslogin

no, but I could probably have worded it better. What I mean to say is that if you do this in JS:

   function add (a, b) {
     return a + b;
   }
and this in C:

  int add(int a,int b)
  {       
     return a + b;
  }
you're going to get essentially the same performance.

The latter sentence you quoted refers to the specific builtin functions that fast.js re-implements. So we first get close to native performance in JS, then we beat the builtin functions by doing less work overall.


gotcha, thanks for the clarification


regarding your edit, you're exactly right, of course a for loop will be faster. Sometimes you really do need a function call though, in which case fast forEach and map implementations become more useful.

The next step for fast.js are some sweet.js macros which will make writing for loops a bit nicer, because it's pretty painful to write this every time you want to iterate over an object:

    var keys = Object.keys(obj),
        length = keys.length,
        key, i;
    for (i = 0; i < length; i++) {
      key = keys[i];
      // ...
    }
I'd rather write:

   every key of obj {
     // ...
   }
and have that expanded at compile time.

Additionally there are some cases where you must use inline for loops (such as when slicing arguments objects, see https://github.com/petkaantonov/bluebird/wiki/Optimization-k...) and a function call is not possible. These can also be addressed with sweet.js macros.


To be fair, it's not obvious if you're not a JS expert: coming from some other language, you could naively assume that function call gets inlined, with no overhead.


A few months ago, I tried some image processing with JS and the canvas object (big Arrays).

They have a structure like image[pixel].color

What I found was, always traversing the object structure was much slower than putting every pixel color as an argument in a function, that gets called every iteration.

So I had the impression, reasonable simple function, like a greyscale filter, get inlined by engines like V8.


You probably would have been better off with typed arrays and fragment shaders, if you were going for performance.


Aren't the arrays produced by a canvas object typed?


Yep, but they only become performant when you treat them as such instead of doing naive e.g. multiplication. See: https://hacks.mozilla.org/2011/12/faster-canvas-pixel-manipu...


Like any other compiler, function calls sometimes get inlined. And sometimes not. Depending on compiler heuristics of various sorts.


Note that 'in'

    for (key in object)
is for getting keys, in Firefox, meanwhile 'of'

     for (value of iterator)
is for getting values out of an Iterator.


Most of the array methods are explicitly defined in terms of indexing rather that iterators, so for(of) is incorrect. On the other hand indexing is still faster than iteration in the major implementations.


My comment were intended as a direct reply to phpnode's code, 'ignoring' the rest of the thread.

How do you mean when you state that for(of) is incorrect?

Perhaps this 'example' clears something up?

  « for (value in {a:1,b:2,c:3,d:4,e:5}) console.log(value)
  » undefined
    "a"
    "b"
    "c"
    "d"
    "e"
  « for (value of {a:1,b:2,c:3,d:4,e:5}) console.log(value)
  × TypeError: ({a:1, b:2, c:3, d:4, e:5})['@@iterator'] is not a function


the only issue is i'd like to differentiate between iterating arrays and objects at the syntax level. Coffeescript uses `in` for arrays and `of` for objects, but I agree that becomes confusing. Open to other suggestions!


I've been spending some time thinking about CoffeeScript's choice for `in` and `of` and I think I have the answer: they should be more regular. One (probably `of`) should be used for the keys/indexes and the other (probably `in`) should be used for contents.

These are legal CoffeeScript examples:

    for key, value of object
    if key of object

    for value in array
    if value in array

    for index, value of array
    if index of array
The one that's missing is:

    for value in object
    if value in object


The issue with coffeescript is that it uses the opposite syntax to JS - `in` for arrays instead of objects. ES6 introduces the `of` keyword for real iterators so i don't really want to use that for either of them. It's tricky. There are other options but they're verbose

    every element as key, value from arr {
      console.log(key, value);
    }

    every property as key, value from obj {
      console.log(key, value);
    }


Thoughts on this?

    every [key, value] from arr {
      console.log(key, value);
    }

    every {key, value} from obj {
      console.log(key, value);
    }

    every [key] from arr {
      console.log(key);
    }

    every {_, value} from obj {
      console.log(value);
    }
Looks a bit cramped to me but I get the impression that array comprehension is one of the major reasons that people use Coffeescript, so perhaps it's not too much trouble?

There's also the issue with some fonts making it hard to differentiate between { and [ but it's an idea that might be worth to think about at least.


I quite like this suggestion. If we flip the key, value around to make it inline with what forEach etc do, then we don't need the placeholder for iterating without the key:

    fast-each [value] from arr {

    }

    fast-each {value} from obj {

    }
 
    fast-each [value, key] from arr {

    }

    fast-each {value, key} from obj {

    }
The disadvantage is that this looks like array comprehension but isn't.

Alternative:

    fast-properties key, value from obj {

    }

    fast-elements index, value from arr {

    }

    fast-properties value from obj {

    }

    fast-elements value from arr {

    }


That's a though one. I know I can't think of any that aren't either verbose or nondescript.

But if Coffescript can walk away with its choice then I don't think you'll have anything to worry about.


if you've ever written a for loop like this, you'd see the exact same issue.

    for (i = 0, total = arr.length; i < total; i++) {
      item = arr[i];
    }
This will also iterate over gaps in the array. Is this a bug?


This is actually unlikely to be a problem. The edge cases ignored here are actually the same as those ignored by underscore.js, which is obviously a very popular library.


I agree that it's unlikely to be a problem...until your site gets popular and a .01% corner case becomes a weekly occurrence or until someone sees that you're using fast and exploits an attack vector.


it's not that kind of edge case. It simply means that some very uncommon constructs are not supported. If you never use those constructs, which basically nobody does, then it will never fail. (and in fact the failure mode is pretty soft and unlikely to crash your application). If your code worked with this once, it will always work.


lodash is the fast one that pioneered this, underscore for the longest time fell back to slow native ones.


you're right in that it's important to consider inputs of different sizes. I did this for the indexOf() functions, I'll do the same here, thanks!


To prove its worth, try showing a Real use case.


this library does not overwrite any native functions or manipulate any prototypes, you have to call the fast.js versions explictly - it's opt in.


Lo-Dash acts exactly the same way.


An alternative interpretation might be - JavaScript, the high level language language that's so awesome that it can out perform native code*

* as long as you make it do less work


Why isn't the native version doing less work? Isn't that what you expect from a language design, that the native version will be as close to optimal as possible in the first place?


Did you read the article? These functions are not 100% equivalent.


Fair enough. Why doesn't the title of the post say so then? "Faster incomplete versions of Javascript native functions" would have been a better summary.

Or even discuss the fact that if this library is good enough for people to use then maybe the edge cases that the native versions are covering might not be so useful after all?


The native versions have to/should 100% support the language spec and historical behavior. In this instance, it means they can't optimize much that iterates over a sparse array.


As well as the example of "sparse arrays" shown in the README, there are some additional slight gotchas around `.bind()`, e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

These fall outside of the common uses of these methods, and most people don't even know they exist.


author of the library here. The native implementations have to follow the spec exactly, which means that they have to guard against specific edge cases. The reimplementations in fast.js do not follow the spec exactly, they optimise for 99.9% of use cases and totally ignore the 0.1%. This, combined with the fact that the JS is JITed straight into machine code anyway gives fast.js a significant advantage - it does less work and so is faster.


Do you have documentation on the differences in behavior?


I think he meant that he had no affiliation himself


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: