Slice vs Substr vs Substring vs [ ] Methods

JavaScript performance comparison

Revision 39 of this test case created by derry

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = "DzjKbdy1fysm5LiEbCNUmbl9wqaAKL0o6Uqh3bIIsqhjelkXhFbrzAhJwLmbp1hxQL7RHWAVXlf7yM0RyvDZhWq0FoLuBdfJkfA4OgKgAMXRyXSDptnYcbx41UQG7soQsh5yDAkwavxw04DT3zpPRc24odAGDQpvaFs2DAaLcjCbt1zyJteK2Aze1ge6mICvsLJahU7qT8Bvq70wJU1hfuFWtrhLlOGUuD7OB6Ot0hegnWwC5i6NL0p7fvzS8SFbgjKDTuhA9dULRauCfIKQ0YFqoLKto9ZXqvLflj2lxisvVbqugfa6eEHwDl0kNIxRO5oskoo2mBi5UU5XcQsnuJ0Z6gSLhrmezg8rW9H5zUfaA1wwtIteULZi8saalfLe7q134DgaPPpfaNsj4c4mSaTVV2df8lZBkUzqqFaYGmZnfAmy4yC0Sa5Uyaky1k72rhPWbX3ui28yS5u6zWlg6ndo036W7fdyriVNryZIstMbiBGbew0zkEHaWxwiLVxzqLVqwEM04kYU2N7ivjdh4GFFgdOsMJl8kVet8VrIrIdqOQlAtVXWtkkL1sbVxTYtcOp25ulNHZphXENjmqTeFJyEd1iEBTew0KTXP5M7kqji8NoNB4OIA2mDkhtRj8z67bUegLs7gDRV2taq3AE0vWGJno86cE9G0Ox6xL2vyi5QsJwP4pywgGZBrYMGj3z0xBCWrglqbLDsrW51o90XjhtQgLaJu80z3LEoc8MEgRRbUlE3qNHN6DcyjFiHF1YZKTlenZ7DnpUiqC3350hlXNzBRfWp5Lrxxmn7KV7okb71DK5KHEgCZdhGSe4djy6aIg0WZGQqEbn69JWO5fJwTAKaVbqM05iQM6EmSvLzFsM4eNFo4gPYVCL8rJ8kSARvgq43x1YooveKbZstWmMG0ZfTm6IazZs2YpxM7gt7YJFtQgPBonXqu3m8duZTy5iL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ekFFu0G97NKrFN07olSJycTfxQ6LaqqOhPakFO5uJs9Tc6XIN0FMuDb4xL8pI46Yv7kUgbK6veYLucDX8jtAPK6Gxb3RlYE35IyYgNAaXP0xa5vw9Tpr95h4Rae0i48jPUJsS0oo8AX5lb0nD1jOn1D792aA1Pd7BjJcUi9PWgoJJEBk1cPBk5OO8DTfbs9Tm78HRJeZFqUSPP51T3BCEsFcj3ehCrf9qvVy57jxtHyXVQF3q7EkWBFOElREkFwpC7wQ1OzeMT44KzBxPineiWC75uj3gtwxrVH2HaeXw1lrh8WvauFb1yhCqHLu";
    var length = str.length;
};
</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
Substring
str.substring(0, length-3);
pending…
substr
str.substr(0,length-3);
pending…
slice
str.slice(0,-3);
pending…
[]
str[str.length-3]+str[str.length-2]+str[str.length-1]
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