For vs ForEach

JavaScript performance comparison

Test case created

Info

Testing differences of character comparison using for and foreach

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', '3'];
    chr = '3';
};
</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
For
for (i = 0; i < validChars.length; i++) {
  if (chr == validChars[i]) {
    return true;
  }
}
return false;
pending…
For each
for (thing in validChars) {
  if (chr == thing) {
    return true;
  }
}
return false;
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