Calling that function on chrome takes less than three times of what it takes to call an empty function f(){}
so I think that you are not going to gain much with any rewriting.
The problem is the function overhead, not the formula. May be inlining it could save something more interesting...
EDIT
To make the test what I did was just opening a console in Chrome (ctrl-shift-C) and created a timing function with
timeit = function(f) {
var start=(new Date).getTime();
for (var i=0; i<100000; i++)
f(1);
return (new Date).getTime() - start;
}
and then tested it with function(){}
and with your function.
It turns out however that this kind of test is very unreliable. I even got absurd results with timeit(f1)
reporting 200 and timeit(f2)
reporting 120 (quite a difference) but f1
and f2
were indeed two variables linked to the same function object. Also there was a difference between timeit(f)
and timeit(function(x){ return Math.cos(x); })
even when f
was exactly that function.
May be there is an explanation because of how V8 and the javascript console interact but I don't know what it is.
Also with FF4 this approach gives very unreliable results...