Created
February 14, 2013 03:51
-
-
Save dai-shi/4950506 to your computer and use it in GitHub Desktop.
benchmark of String.startsWith equivalents in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
Benchmark.prototype.setup = function() { | |
a = ["test"]; | |
for (var i = 0; i < 10000; i++) { | |
a.push("some other stuff"); | |
} | |
s = a.join(); | |
re1 = new RegExp("^test"); | |
re2 = new RegExp("^not there"); | |
}; | |
var suite = new Benchmark.Suite(); | |
// add tests | |
suite.add('indexOf', function() { | |
var r1 = (s.indexOf("test") === 0); | |
var r2 = (s.indexOf("not there") === 0); | |
}) | |
.add('lastIndexOf', function() { | |
var r1 = (s.lastIndexOf("test", 0) === 0); | |
var r2 = (s.lastIndexOf("not there", 0) === 0); | |
}) | |
.add('substring', function() { | |
var r1 = (s.substring(0, "test".length) == "test"); | |
var r2 = (s.substring(0, "not there".length) == "not there"); | |
}) | |
.add('slice', function() { | |
var r1 = (s.slice(0, "test".length) == "test"); | |
var r2 = (s.slice(0, "not there".length) == "not there"); | |
}) | |
.add('regex', function() { | |
var r1 = (/^test/).test(s); | |
var r2 = (/^not there/).test(s); | |
}) | |
.add('compiled regex', function() { | |
var r1 = re1.test(s); | |
var r2 = re2.test(s); | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ | |
'async': false | |
}); |
indexOf x 12,782 ops/sec ±81.75% (13 runs sampled)
lastIndexOf x 7,163,640 ops/sec ±7.38% (60 runs sampled)
substring x 10,142,306 ops/sec ±10.17% (55 runs sampled)
slice x 7,855,020 ops/sec ±10.90% (56 runs sampled)
regex x 9,497,811 ops/sec ±6.89% (62 runs sampled)
compiled regex x 10,821,279 ops/sec ±6.57% (69 runs sampled)
Fastest is compiled regex,substring
node v0.10.35
何度か試行しましたが、compiled regex, substringとなるケースが多いようでした。
Update April 2020 since this is still the first Google result for node js startswith performance
.
indexOf 113,625 ops/sec ±3.51% (78 runs sampled)
startsWith 22,522,847 ops/sec ±0.20% (94 runs sampled)
lastIndexOf 19,520,195 ops/sec ±0.41% (92 runs sampled)
substring 1,368,227,502 ops/sec ±0.07% (95 runs sampled)
slice 1,360,307,286 ops/sec ±0.07% (93 runs sampled)
regex 26,319,865 ops/sec ±1.17% (91 runs sampled)
comp regex 19,868,632 ops/sec ±3.01% (87 runs sampled)
Fastest is substring
Added str.startsWith()
and tested on Node v12.16.2
.
The edited code is available HERE
How to run this via command line?
$ time node starts-with.js
indexOf:
startsWith:
lastIndexOf:
substring:
slice:
regex:
compiled regex:
>Fastest is
node starts-with.js 0.10s user 0.01s system 99% cpu 0.117 total
You can use repeat()
to multiply a string
> 'abc'.repeat(100)
'abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc'
I had to use simplification method
var str_repeat = 100
var checks = 1000000
var start = 'start text'
var not_there = 'not there'
var str = start + 'some string'.repeat(str_repeat)
console.time()
for (let i = 0; i < checks; i++)
str.startsWith(start)
console.timeEnd()
$ time node starts-with.js
default: 63.477ms
node starts-with.js 0.09s user 0.01s system 98% cpu 0.100 total
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
indexOf x 4,724 ops/sec ±8.30% (51 runs sampled)
lastIndexOf x 1,970,353 ops/sec ±0.22% (67 runs sampled)
substring x 2,348,729 ops/sec ±5.86% (62 runs sampled)
slice x 2,009,238 ops/sec ±5.15% (62 runs sampled)
regex x 2,140,300 ops/sec ±1.21% (64 runs sampled)
compiled regex x 2,241,266 ops/sec ±0.96% (62 runs sampled)
Fastest is compiled regex,slice
I suppose lastIndexOf must be the fastest, which is true for Firefox (see: http://jsperf.com/string-startswith/3 )