split vs regexp classname

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var s = ' class   class-name ';
};

Benchmark.prototype.teardown = function() {
    if (s !== 'class') throw new Error('Unexpected string!');
};
</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
split
var a = s.split(' '),
    r = [],
    p, i = 0,
    l = a.length;

for (; i < l; ++i) {
  p = a[i];
  if (p && p !== 'class-name') {
    r.push(p);
  }
}
s = r.join(' ');
pending…
regexp
s = s.replace(/\bclass\-name\b/g, '').replace(/\s{2,}/g, ' ').replace(/^\s/g, '').replace(/\s$/g, '');
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