Optional parameters perf
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function fn1(arg1, arg2, arg3) {
switch(arguments.length) {
case 2:
arg3 = 'sample';
case 1:
arg2 = 310;
case 0:
arg1 = /five/;
}
}
function fn2(arg1, arg2, arg3) {
if(arg1 === undefined) arg1 = /five/;
if(arg2 === undefined) arg2 = 310;
if(arg3 === undefined) arg3 = 'sample'
}
function fn3(arg1, arg2, arg3) {
if(typeof arg1 === 'undefined') arg1 = /five/;
if(typeof arg2 === 'undefined') arg2 = 310;
if(typeof arg3 === 'undefined') arg3 = 'sample'
}
function fn4(arg1, arg2, arg3) {
if(arguments.length === 0) arg1 = /five/;
if(arguments.length <= 1) arg2 = 310;
if(arguments.length <= 2) arg3 = 'sample'
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Switch |
|
pending… |
If-Undefined |
|
pending… |
If-Typeof-Undefined |
|
pending… |
Arg-length |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
- Revision 2: published
0 comments