by binarymax · 12y ago · 0 comments
function doesntLeakArguments() {
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
return args;
}
becomes: function doesntLeakArguments() {
var len = arguments.length;
var args = new Array(len);
for(var i = 0; i < len; ++i) {
args[i] = arguments[i];
}
return args;
}
And also, if you've got a switch statement with more than 128 cases, you've probably got bigger problems on your hands than v8 optimizations.I see some things in here that I can start switching to immediately for some of my node modules.
In the Javascript world, "Your Mileage May Vary" truly is a motto.
Using a hashtable for instruction dispatch in an emulator would be utterly insane. You'd use an array (but the compiler can do a better job by generating raw asm for a jump table.)
It looks like you're right, very right unless there's something else going on in the perf tests.
1) It shouldn't exercise the GC. GC pressure totally skews jsperf results in a way it can't compensate for.
2) It should compute a result on every iteration that is checked at the end to make sure it is accurate. (This ensures that the compiler can't optimize out unused results from your benchmark loop)
3) The check should have actual control flow so it can't be ignored.
It's also worthwhile to warm all your test cases once in the setup portion of the page before the loop runs. Some performance issues only appear once you've passed multiple argument types into a function, so if you run a test case that passes in ints, and then run a test case that passes in floats, the second test case will be slower for no obvious reason. You can swap the test cases around and the second run will still be slower.
(There are actually a huge list of problems with javascript microbenchmarks, and jsperf only adds to them by being low quality software. Please use it sparingly.)
for(var i=0;i<a.length;i++) if(check(a[i])) a.push('foo');