Slice vs Substr vs Substring vs [ ] Methods (v38)

Revision 38 of this benchmark created on


Description

This is comparing the runtimes of [ ], substr, substring, and slice in a string for large (10000 character) string size.

Setup

var longString = "",
        word = "",
        length,
        offset,
        start,
        end,
        i;
    
    for (i = 0; i < 10000; i++) {
        longString += i % 10;
    }
    
    length = longString.length;
    start = offset = Math.floor(length / 4);
    end = length - offset;
    
    length = end - start;

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
longString.substring(start, end);
ready
Slice (long)
longString.slice(start, end);
ready
Substr (long)
longString.substr(start, length);
ready
[ ] (long)
for (i = start; i < length; i += 1) {
    word += longString[i];
}
ready
charAt (long)
for (i = start; i < length; i += 1) {
    word += longString.charAt(i);
}
ready

Revisions

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