Hi all,
In the 1.6.1 source, we're grabbing a reference to Array's `slice`
method and then using that in a variety of places to copy subsets (or
sometimes entire sets) of arguments, like so:
var slice = Array.prototype.slice;
//. ...
function bind(context) {
if (arguments.length < 2 && Object.isUndefined(arguments[0]))
return this;
var __method = this, args = slice.call(arguments, 1);
return function() {
var a = merge(args, arguments);
return __method.apply(context, a);
}
}
This is presumably on the assumption that using the built-in slice
method will perform better than our own loop would. It's a perfectly
reasonable assumption. I *like* the assumption. The assumption makes
*sense* to me.
It just happens to be wrong:
http://pastie.org/605898
(Well, except on Firefox.)
On Chrome, using Array.prototype.slice via Function#call is 8-10 TIMES
slower than our own function would be; on IE, it's about 25% slower,
on Safari and Opera it's about 50% slower. On Firefox, the odd one
out, the "own loop" is 50% slower than `slice`.
I mention this for three reasons.
1. We might consider providing a copy function like the one in that
pastie and using it in the various places we copy args skipping the
first few. It adds code, but improves performance -- except in
Firefox -- in some key areas (binding, currying, etc.), and
performance is an issue.
2. It's a reminder to check our assumptions.
3. It raises a question I've had for a while: How do you "feature
test" performance stuff? This isn't the only thing like this.
There's this question/answer[1] over on stackoverflow, for instance.
Writing my originally-brief answer I made a reasonable assumption
about performance, someone checked it and found that I was Quite Wrong
Thank You, and that lead to my finding wild differences in a very
simple operation (zero-filling an array) across implementations.
FWIW,
[1]
http://stackoverflow.com/questions/1295584/most-efficient-way-to-create-a-zero-filled-javascript-array/1295671
--
T.J. Crowder
tj / crowder software / com
www.crowdersoftware.com