Skip to content

Instantly share code, notes, and snippets.

@Snack-X
Last active August 29, 2015 14:27
Show Gist options
  • Save Snack-X/1e14bad01071dbf9ba1b to your computer and use it in GitHub Desktop.
Save Snack-X/1e14bad01071dbf9ba1b to your computer and use it in GitHub Desktop.
String startsWith() benchmark
var assert = require("assert");
var Benchmark = require("benchmark");
// Setup
var shortString = "startsWith";
var longString = ["startsWith"];
for(var i = 0 ; i < 10000 ; i++) longString.push(" and some dummy strings");
longString = longString.join();
// Candidates
function startsWith1(str, find) { return str.indexOf(find) === 0; }
function startsWith2(str, find) { return str.lastIndexOf(find) === 0; }
function startsWith3(str, find) { return str.substring(0, find.length) === find; }
function startsWith4(str, find) { return str.slice(0, find.length) === find; }
function startsWith5(str, find) { return new RegExp("^" + find).test(str); }
function runSuite(testString) {
var suite = new Benchmark.Suite;
suite
.add("indexOf", function() {
assert(startsWith1(testString, "start"));
assert(!startsWith1(testString, "not"));
})
.add("lastIndexOf", function() {
assert(startsWith2(testString, "start"));
assert(!startsWith2(testString, "not"));
})
.add("substring", function() {
assert(startsWith3(testString, "start"));
assert(!startsWith3(testString, "not"));
})
.add("slice", function() {
assert(startsWith4(testString, "start"));
assert(!startsWith4(testString, "not"));
})
.add("regex", function() {
assert(startsWith5(testString, "start"));
assert(!startsWith5(testString, "not"));
})
.add("manual", function() {
assert(startsWith6(testString, "start"));
assert(!startsWith6(testString, "not"));
})
.add("manual2", function() {
assert(startsWith7(testString, "start"));
assert(!startsWith7(testString, "not"));
})
.on("cycle", function(ev) {
console.log(String(ev.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").pluck("name"));
});
if(typeof String.prototype.startsWith === "function") {
suite.add("native", function() {
assert(testString.startsWith("start"));
assert(!testString.startsWith("not"));
});
}
suite.run({ async: false });
}
console.log("test on short string\n");
runSuite(shortString);
console.log("\n\ntest on long string\n");
runSuite(longString);
String.prototype.startsWith = function(needle) {
var len = needle.length, i = 0;
for( ; i < len ; i++) {
if(this.charCodeAt(i) !== needle.charCodeAt(i)) return false;
}
return true;
};
# Mac OS X 10.10.8
# iMac (Mid 2011) / Intel Core i5-2400S @ 2.50 GHz
$ node -v
v0.12.7
$ node bench.js
test on short string
indexOf x 8,847,966 ops/sec ±0.47% (96 runs sampled)
lastIndexOf x 9,462,469 ops/sec ±0.39% (96 runs sampled)
substring x 13,577,971 ops/sec ±0.34% (99 runs sampled)
slice x 13,202,458 ops/sec ±0.71% (92 runs sampled)
regex x 2,333,589 ops/sec ±1.94% (92 runs sampled)
Fastest is substring
test on long string
indexOf x 2,238 ops/sec ±0.87% (95 runs sampled)
lastIndexOf x 1,544 ops/sec ±0.87% (97 runs sampled)
substring x 6,994,594 ops/sec ±1.25% (98 runs sampled)
slice x 7,113,935 ops/sec ±0.62% (94 runs sampled)
regex x 2,512,370 ops/sec ±0.59% (98 runs sampled)
Fastest is slice,substring
$ iojs -v
v3.1.0
$ iojs -p process.versions.v8
3.31.74.1
$ iojs bench.js
test on short string
indexOf x 8,329,556 ops/sec ±0.55% (99 runs sampled)
lastIndexOf x 8,964,877 ops/sec ±0.45% (100 runs sampled)
substring x 11,810,505 ops/sec ±0.42% (97 runs sampled)
slice x 12,331,035 ops/sec ±0.46% (99 runs sampled)
regex x 2,355,553 ops/sec ±0.86% (97 runs sampled)
native x 5,361,060 ops/sec ±0.55% (98 runs sampled)
Fastest is slice
test on long string
indexOf x 4,844 ops/sec ±0.53% (96 runs sampled)
lastIndexOf x 1,593 ops/sec ±0.41% (100 runs sampled)
substring x 12,125,731 ops/sec ±0.53% (95 runs sampled)
slice x 12,208,405 ops/sec ±0.41% (99 runs sampled)
regex x 2,376,476 ops/sec ±0.54% (100 runs sampled)
native x 4,867 ops/sec ±0.53% (97 runs sampled)
Fastest is slice
# Ubuntu 14.04.3 LTS
# Intel Core i3-4330 @ 3.50GHz
$ node -v
v0.12.7
$ node bench.js
test on short string
indexOf x 9,854,250 ops/sec ±0.86% (99 runs sampled)
lastIndexOf x 11,025,935 ops/sec ±0.36% (103 runs sampled)
substring x 18,437,986 ops/sec ±0.63% (101 runs sampled)
slice x 18,562,035 ops/sec ±0.46% (102 runs sampled)
regex x 2,800,253 ops/sec ±0.53% (101 runs sampled)
Fastest is slice,substring
test on long string
indexOf x 7,732 ops/sec ±0.05% (102 runs sampled)
lastIndexOf x 2,313 ops/sec ±0.51% (85 runs sampled)
substring x 18,280,238 ops/sec ±0.61% (99 runs sampled)
slice x 18,718,036 ops/sec ±0.27% (103 runs sampled)
regex x 2,736,704 ops/sec ±0.20% (103 runs sampled)
Fastest is slice
$ iojs -v
v3.1.0
$ iojs -p process.versions.v8
3.31.74.1
$ iojs bench.js
test on short string
indexOf x 12,366,352 ops/sec ±0.60% (97 runs sampled)
lastIndexOf x 13,597,687 ops/sec ±0.61% (97 runs sampled)
substring x 17,181,996 ops/sec ±0.71% (92 runs sampled)
slice x 17,747,238 ops/sec ±0.86% (96 runs sampled)
regex x 3,024,220 ops/sec ±0.51% (102 runs sampled)
native x 7,789,762 ops/sec ±0.72% (98 runs sampled)
Fastest is slice
test on long string
indexOf x 7,688 ops/sec ±0.06% (102 runs sampled)
lastIndexOf x 4,239 ops/sec ±0.70% (100 runs sampled)
substring x 17,526,103 ops/sec ±0.30% (103 runs sampled)
slice x 17,435,046 ops/sec ±1.04% (96 runs sampled)
regex x 2,984,354 ops/sec ±0.25% (103 runs sampled)
native x 7,694 ops/sec ±0.07% (103 runs sampled)
Fastest is substring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment