Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Arguments vs. Rest
(version: 1)
Which is faster to check if function has not been passed too many arguments?
Comparing performance of:
arguments vs rest params
Created:
9 years ago
by:
Registered User
Jump to the latest result
Tests:
arguments
var GAS = 0; function run(init, runner) { GAS = init; var frames = [{ run: runner }]; var thisFrame = frames.pop(); while(true) { var res = thisFrame.run(thisFrame); if(res.isCont) { var len = res.frames.length; for(var i = 0; i < len; i++) { frames.push(res.frames.pop()); } GAS = init; thisFrame = frames.pop(); } else { if(frames.length <= 0) { break; } thisFrame = frames.pop(); thisFrame.ans = res; } } return res; } var linkNames = ["first", "rest"]; function link(f, r) { if (arguments.length > 2) { var $a=new Array(arguments.length); for (var $i=0;$i<arguments.length;$i++) { $a[$i]=arguments[$i]; } throw new Error("Failed: " + $a); } return { $name: "link", $fields: linkNames, first: f, rest: r } } var empty = { $name: "empty" }; function map(f, l) { if (arguments.length > 2) { var $a=new Array(arguments.length); for (var $i=0;$i<arguments.length;$i++) { $a[$i]=arguments[$i]; } throw new Error("Failed: " + $a); } var step = 0; if(f.isFrame) { var ans = f.ans; step = f.step; var t1 = f.vars[0]; var fst = f.vars[1]; var rst = f.vars[2]; var t2 = f.vars[3]; l = f.args[1]; f = f.args[0]; } if(GAS-- <= 0) { return { isCont: true, frames: [{ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }] }; } while(true) { switch(step) { case 0: var dispatch = { empty: 1, link: 2 }; step = dispatch[l.$name]; break; case 1: ans = empty step = 5 break; case 2: fst = l[l.$fields[0]]; rst = l[l.$fields[1]]; step = 3; ans = f(fst); break; case 3: t1 = ans; step = 4 ans = map(f, rst); break; case 4: t2 = ans; step = 5 ans = link(t1, t2); break; case 5: GAS++; return ans; } if(ans && ans.isCont) { ans.frames.push({ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }); return ans; } } } var buildList = function(n) { if (arguments.length > 1) { var $a=new Array(arguments.length); for (var $i=0;$i<arguments.length;$i++) { $a[$i]=arguments[$i]; } throw new Error("Failed: " + $a); } var l = empty; for(var i = 0; i < n; i++) { l = link(i, l); } return l; } run(100, function() { return map(function(l) { return l + 1; }, buildList(4000)); });
rest params
var GAS = 0; function run(init, runner) { GAS = init; var frames = [{ run: runner }]; var thisFrame = frames.pop(); while(true) { var res = thisFrame.run(thisFrame); if(res.isCont) { var len = res.frames.length; for(var i = 0; i < len; i++) { frames.push(res.frames.pop()); } GAS = init; thisFrame = frames.pop(); } else { if(frames.length <= 0) { break; } thisFrame = frames.pop(); thisFrame.ans = res; } } return res; } var linkNames = ["first", "rest"]; function link(f, r, ...rest) { if (rest.length !== 0) { throw new Error("Failed: " + rest); } return { $name: "link", $fields: linkNames, first: f, rest: r } } var empty = { $name: "empty" }; function map(f, l, ...rest) { if (rest.length !== 0) { throw new Error("Failed: " + rest); } var step = 0; if(f.isFrame) { var ans = f.ans; step = f.step; var t1 = f.vars[0]; var fst = f.vars[1]; var rst = f.vars[2]; var t2 = f.vars[3]; l = f.args[1]; f = f.args[0]; } if(GAS-- <= 0) { return { isCont: true, frames: [{ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }] }; } while(true) { switch(step) { case 0: var dispatch = { empty: 1, link: 2 }; step = dispatch[l.$name]; break; case 1: ans = empty step = 5 break; case 2: fst = l[l.$fields[0]]; rst = l[l.$fields[1]]; step = 3; ans = f(fst); break; case 3: t1 = ans; step = 4 ans = map(f, rst); break; case 4: t2 = ans; step = 5 ans = link(t1, t2); break; case 5: GAS++; return ans; } if(ans && ans.isCont) { ans.frames.push({ run: map, isFrame: true, vars: [t1, fst, rst, t2], args: [f, l], step: step }); return ans; } } } var buildList = function(n, ...rest) { if (rest.length !== 0) { throw new Error("Failed: " + rest); } var l = empty; for(var i = 0; i < n; i++) { l = link(i, l); } return l; } run(100, function() { return map(function(l) { return l + 1; }, buildList(4000)); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
arguments
rest params
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Android 16; Mobile; rv:148.0) Gecko/148.0 Firefox/148.0
Browser/OS:
Firefox Mobile 148 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
arguments
2108.4 Ops/sec
rest params
2444.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
A complex JavaScript benchmark! After analyzing the code, I'll provide some insights on the test results. The test consists of two variations: 1. **rest params**: This variation tests the function `map` with variable arguments (i.e., rest parameters) using the spread operator (`...rest`). The test calls `buildList` with a large number of elements and passes the result to `map`, which increments each element by 1. 2. **arguments**: This variation is not explicitly mentioned in the code snippet, but it's likely another test that uses the traditional argument list syntax (i.e., multiple arguments separated by commas). The results show two browsers (Chrome 125) executing these tests with different execution rates: **rest params**: 1669 executions per second **arguments**: 1401 executions per second Based on this data, it appears that Chrome 125 performs better when using rest parameters than traditional argument list syntax. This could be due to various factors, such as: * JavaScript engine optimizations: Modern JavaScript engines like V8 (used by Chrome) might optimize rest parameter parsing and handling more efficiently than traditional argument list parsing. * Context switching: When using traditional arguments, the interpreter needs to switch context more frequently to access each argument, which can lead to slower performance. Rest parameters, on the other hand, are handled more aggressively by the engine, reducing context switching. Please note that this is a simplified analysis and might not reflect the actual reasons behind these results. More in-depth profiling or testing would be required to determine the specific factors contributing to these differences.
Related benchmarks:
Array loop vs foreach vs map (Memory Check)
Array loop vs foreach vs map vs while
Array loop vs foreach vs map -2
Array loop vs foreach vs map with large array
Array loop vs foreach vs map vs while vs some vs every vs filter vs find
Comments
Confirm delete:
Do you really want to delete benchmark?