Hacker News new | ask | show | jobs
by mraleph 4634 days ago
Answer is simple: you are adding more and more listeners as jsPerf runs your test case.

One of the most important things to remember while using jsPerf is that setup phase can and will be executed multiple times. As the result the list of listeners attached to the DOM node is growing and this in turn slows down the event dispatch.

"No return" case is run second so the list of listeners is already large and thus it is slower than "return case".

You should either unregister listeners in tear down phase or register them only once at global initialization time. Here is the fixed variant:

http://jsperf.com/always-return-on-jquery-events/28

[also from the JavaScript VM point of view function () { } and function () { return; } are completely the same]

3 comments

I love that you proved this wrong so quickly. Saved me from going and adding returns across a bunch of projects. Thanks!
Also another mistake here is the classical "if you want to measure x, don't measure y".

If the OP wanted to measure function with or without return , even without realizing how futile that is, they still should not include things like jQuery.

Well, before the GP post, my takeaway was that it's better to return when using jQuery. The justification for this would have been some magic, which is the sole purpose of jQuery, slowing stuff down.

And the web being as it is, testing with jQuery might even be more useful than testing without it.

That is a common fallacy and benchmarking anti-pattern, and it really enrages me that some people look at the completely incorrect results and justify it with "I am going to be using this with jQuery so why shouldn't I add some jQuery method calls". And you are denying this despite the "fixed" js perf showing different results e.g. on chrome 29.

You should read http://zedshaw.com/essays/programmer_stats.html

I think you misunderstood. I'm denying nothing.

What I meant was that if I'm going to run a website with jQuery calls, I should absolutely test with jQuery calls. It makes no sense to claim that one should, say, always use short variable names, if one then ships with google closure.

Well at least now I know how to manipulate JSPerf results in a non-obvious way :X