Code Selection: property lookup+function call vs if-else
JavaScript performance comparison
Info
The idea behind this is that we switch the codepath that we're running only very rarely. Should we make the actual codepath selection every time we loop or only at the time we actually change it?
(Test 1 implicates that we use (at the time of change) if-else to change the module into some other module which also has the func property.)
This test tries to see if which code selection strategy has the bigger penalty.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var counterTestModule = (function() {
var value =0;
var func = function() {
value = value + 1;
}
var getValue = function() {
return value;
}
var reset = function() {
value = 0;
}
return {
func: func,
getVal: getValue,
reset: reset
}
})();
var test1 = 'PLUSONE';
var test2 = 'PLUSTWO';
var test3 = 'PLUSTHREE';
var value = 0;
};
Benchmark.prototype.teardown = function() {
value = 0;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Property lookup and function call |
|
pending… |
If-else, when first one hits |
|
pending… |
If-else, when second one hits |
|
pending… |
If-else, when third one hits |
|
pending… |
If-else, when first one hits, with different operand to sum (just in case...) |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments