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 | |
}); |
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
Update April 2020 since this is still the first Google result for
node js startswith performance
.Added
str.startsWith()
and tested onNode v12.16.2
.The edited code is available HERE