brackets vs charAt (v2)

Revision 2 of this benchmark created by bergi on


Description

why [] is slower than charAt (with smaller strings than in the first tests)

Setup

var s = '', ok = ' h dfb  ds84 78sgf  ydf hjb////**', lc = 0, cc = 0
    
    for (var i = 0; i < 3000; i++) {
        s += ok[Math.floor(Math.random() * ok.length)]
    }
    var so = new String(s);

Teardown


    console.log(lc, cc)
    
  

Test runner

Ready to run.

Testing in
TestOps/sec
property access on primitive
for (var i = 0; i < s.length; i++) {
        if (s[i] == '/' && s[i+1] == '/') lc++, i++
        if (s[i] == '/' && s[i+1] == '*') cc++, i++
}
ready
charAt on primitive
for (var i = 0; i < s.length; i++) {
        if (s.charAt(i) == '/' && s.charAt(i+1) == '/') lc++, i++
        if (s.charAt(i) == '/' && s.charAt(i+1) == '*') cc++, i++
}
ready
property access on object
for (var i = 0; i < s.length; i++) {
        if (so[i] == '/' && so[i+1] == '/') lc++, i++
        if (so[i] == '/' && so[i+1] == '*') cc++, i++
}
ready
charAt on object
for (var i = 0; i < s.length; i++) {
        if (so.charAt(i) == '/' && so.charAt(i+1) == '/') lc++, i++
        if (so.charAt(i) == '/' && so.charAt(i+1) == '*') cc++, i++
}
ready

Revisions

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

  • Revision 1: published by exebook on
  • Revision 2: published by bergi on