Hacker News new | ask | show | jobs
by trevnorris 4686 days ago
> my closures are executed oh, about 100 times a second (e.g. completely negligible perf hit). This is a seriously flawed benchmark.

Take Node's case where you're accepting HTTP requests. Each request is going to require a new instance, and call into the connection listener. If the connection listener contains all the function declarations for all events attached to the client then you loose the performance you could have had by v8 using a previously optimized function. The benchmark was intentionally made to show worst case. I don't expect every application to magically run 80% faster just because they moved some function declarations around.

1 comments

I'm actually surprised at how low the cost of function declarations is. V8 can handle millions of function declarations per second (as you know http://jsperf.com/closure-vs-callback/8). The cost is so low that ArraySort in http://code.google.com/p/v8/source/browse/trunk/src/array.js, which implements Array.prototype.sort() in V8, declares 6 functions inside a function. Sure, you shouldn't define functions in tight loops, but otherwise this seems like unnecessary optimization.