将参数添加到“参数”数组对象,性能testing
你好,我做了一些简单的testing:更好的方式添加参数在javascript中的数组类似的对象:
这表明使用Array.prototype.push.call比Chrome慢3倍左右,为什么?
http://jsfiddle.net/vhrs56nm/
function test() { Array.prototype.push.call(arguments, 123); } function test2() { arguments[arguments.length] = 123; arguments.length++; } console.time("test1"); for ( var i=0; i<1000000; i++ ) { test(1,2,3); } console.timeEnd("test1"); console.time("test2"); for ( var i=0; i<1000000; i++ ) { test2(1,2,3); } console.timeEnd("test2");
Array.prototype.push.call
比Chrome慢3倍左右,为什么?
因为
- 这是一个函数调用(实际上是两个),而这些调用仍然会花费一些东西
- 传递
arguments
需要对杀死优化的对象进行具体化 - 死代码的去除比较复杂