for-length-optimization

JavaScript performance comparison

Test case created by Inez Korczyński

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var mw = {}
    mw.page = {};
    mw.page.protection = {};
    mw.page.protection.levels = [];
   
    for (var i = 0; i < 100; i++) {
      mw.page.protection.levels[i] = i;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
without-optimization
var level;
for (var i = 0; i < mw.page.protection.levels.length; i++) {
  level = mw.page.protection.levels[i];
  if (level % 2 === 0) {
    console.log("...");
  }
}
pending…
with-optimization-1
var level, len, levels = mw.page.protection.levels;

for (var i = 0, len = levels.length; i < len; i++) {
  level = levels[i];
  if (level % 2 === 0) {
    console.log("...");
  }
}
pending…
with-optimization-2
var level, levels = mw.page.protection.levels,
    len = levels.length;

for (var i = 0; i < len; i++) {
  level = levels[i];
  if (level % 2 === 0) {
    console.log("...");
  }
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment