Compare strings, Arrays or Typed Arrays?

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var length = 100000;
    var a1 = new Array(length);
    var i = length;
    while (i--) a1[i] = Math.floor((1 < 16) * Math.random());
    a2 = a1.slice(0);
   
    s1 = a1.map(function(v) {
      return String.fromCharCode(v);
    }).join('');
    s2 = a2.map(function(v) {
      return String.fromCharCode(v);
    }).join('');
   
    ta1 = new Uint16Array(a1);
    ta2 = new Uint16Array(a2);
};
</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
string equals
result = s1 === s2;
pending…
string loop
i = length;
result = true;
while (i--)
if (s1[i] !== s2[i]) {
  result = false;
  break;
}
pending…
Array loop
i = length;
result = true;
while (i--)
if (a1[i] !== a2[i]) {
  result = false;
  break;
}
pending…
Typed Array loop
i = length;
result = true;
while (i--)
if (ta1[i] !== ta2[i]) {
  result = false;
  break;
}
pending…
string loop charCodeAt
i = length;
result = true;
while (i--)
if (s1.charCodeAt(i) !== s2.charCodeAt(i)) {
  result = false;
  break;
}
pending…
string equals2
result = s1 == s2;
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:

0 comments

Add a comment