Live data from Hacker News

Show HN: Fast.js – faster reimplementations of native JavaScript functions

github.com

121–130 of 168 posts

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#121

It's fairly well-known that Array#forEach is up to an order of magnitude slower than a for-loop, across browsers. The usual reason given is the overhead of invoking a closure from the VM. A JS implementation of forEach ought to be somewhere in the middle. The speed-up for "concat" is surprising to me. I wonder if it holds for "splice" and if that is true across browsers.

And it can be made even faster ;)

http://jsperf.com/fast-iter

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#122
post #66

Earlier quoted context omitted.

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),…

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.

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

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#123
post #112

I wonder which projects does actually need that. Hey I'm not bashing here, I thing it's kind cool for learning purposes attempts to do such thing, but I truly wonder if there is an actual production need for such thing.

Game engines, anything that needs to do cycles at a high rate (rendering, algorithms, etc)

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#124

Earlier quoted context omitted.

One other micro optimization for you. Assuming the length of the array does not change in the operation, this is usually faster: for (var j=0, len=input.length; j This prevents rechecking the array length on every iteration.

I actually didn't get any speedup with that one -- looks like V8 can optimize that already. But you're right, that's an important one on some browsers. You can squeeze out another factor of 2 with typed arrays: var input_typed = new Uint32Array(input) exports['numeric typed array'] = function() { var acc = 0; for (var j=0; j And probably more still if you can figure out the asm.js incantation that makes everything st…

The optimisation mentioned by the grandparent is specific to looping overso-called "live" collections of the DOM. NodeList [0] is sometimes a live collection, and is the return type of querySelectorAll, so it's likely you've dealt with this type of collection.

The reason it incurs an overhead is because the DOM is traversed every single time the property is read, to ensure nothing has changed. You can see why caching the length is a reasonable optimisation, as you're unlikely to be modifying the collection while looping.

[0] https://developer.mozilla.org/en/docs/Web/API/NodeList

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#125
post #80

Earlier quoted context omitted.

> start a project or feature with the coding standard of, "use the fast ones" Ironically, that involves certain habit changes that completely obviate the library: 1) avoid map. Just create an array and write a for loop directly at the callsite, putting the code in a block rather than as a separate function 2) avoid indexOf. For single-character indexOf, it's much faster and much more efficient to match the character…

I have a feeling that certain things (e.g., reduce) will suddenly become much more performant with tail call optimization coming in ES6.

Would tail call optimization help in this case? AFAIK JS's iteration methods aren't recursive.

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#126
Today i was refactoring some js code that rendered a graph to svg. It was taking several seconds in some cases, and the developer had done a bunch of micro-optimizations, inlining functions, building caches to avoid nested loops, and so on.

I ran a profile. The code ran for 2.5 seconds, 4 ms of which in the micro-optimized js code, the rest updating the dom, five times all over again. Needless to say that i threw out all the micro-optimizations, halving the number of lines, and fixed it so the dom was updated once.

Anyway, the point i'm making is this: you should micro-optimize for readability and robustness, not performance, unless profiling shows it's worth it. I haven't known a case where pure (non-dom, non-xhr) js code needed micro-optimization for performance in half a decade.

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#127
post #116

As a person who works on a JS engine I can say that a lot of the speed up in this library is the failure to handle holes correctly - it's surprisingly expensive to do a hole check, although there's still room in most engines to optimise it, those checks are fairly time consuming :-/

This seems like a potential win for JS performance in real-world applications -- an optimization hint to indicate whether an array is "overwhelmingly full of holes" or something less sparse where more optimized versions of the functions can be used.

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#128
post #116

As a person who works on a JS engine I can say that a lot of the speed up in this library is the failure to handle holes correctly - it's surprisingly expensive to do a hole check, although there's still room in most engines to optimise it, those checks are fairly time consuming :-/

This seems like a potential win for JS performance in real-world applications -- an optimization hint to indicate whether an array is "overwhelmingly full of holes" or something less sparse where more optimized versions of the functions can be used.

But then you need to check if you need to alter the flag that says whether the array is full of holes, which is itself an extra cost. It's hard to know what's an overall win, adding cost to save it elsewhere.

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#129
post #126

Today i was refactoring some js code that rendered a graph to svg. It was taking several seconds in some cases, and the developer had done a bunch of micro-optimizations, inlining functions, building caches to avoid nested loops, and so on. I ran a profile. The code ran for 2.5 seconds, 4 ms of which in the micro-optimized js code, the rest updating the dom, five times all over again. Needless to say that i threw out…

An ignorant question -- why do people prefer to render graphics to SVG/DOM, when that's such a noticeable performance bottleneck?

Re: Show HN: Fast.js – faster reimplementations of native JavaScript functions

#130
post #126

Today i was refactoring some js code that rendered a graph to svg. It was taking several seconds in some cases, and the developer had done a bunch of micro-optimizations, inlining functions, building caches to avoid nested loops, and so on. I ran a profile. The code ran for 2.5 seconds, 4 ms of which in the micro-optimized js code, the rest updating the dom, five times all over again. Needless to say that i threw out…

An ignorant question -- why do people prefer to render graphics to SVG/DOM, when that's such a noticeable performance bottleneck?

I can't speak for others, but if you write browser apps/websites for a generic audience you basically have no choice. The reason consist of two letters: IE
Post reply on HN