Last active
December 8, 2017 08:40
-
-
Save arccoza/3505f93def05b634ff054fc6a0d7080d to your computer and use it in GitHub Desktop.
Fastest way to fill a string #jsbench #jsperf (http://jsbench.github.io/#3505f93def05b634ff054fc6a0d7080d) #jsbench #jsperf
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Fastest way to fill a string #jsbench #jsperf</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
let n = 120000; | |
String.prototype.repeatPolyfill = function(count) { | |
'use strict'; | |
if (this == null) { | |
throw new TypeError('can\'t convert ' + this + ' to object'); | |
} | |
var str = '' + this; | |
count = +count; | |
if (count != count) { | |
count = 0; | |
} | |
if (count < 0) { | |
throw new RangeError('repeat count must be non-negative'); | |
} | |
if (count == Infinity) { | |
throw new RangeError('repeat count must be less than infinity'); | |
} | |
count = Math.floor(count); | |
if (str.length == 0 || count == 0) { | |
return ''; | |
} | |
// Ensuring count is a 31-bit integer allows us to heavily optimize the | |
// main part. But anyway, most current (August 2014) browsers can't handle | |
// strings 1 << 28 chars or longer, so: | |
if (str.length * count >= 1 << 28) { | |
throw new RangeError('repeat count must not overflow maximum string size'); | |
} | |
var rpt = ''; | |
for (var i = 0; i < count; i++) { | |
rpt += str; | |
} | |
return rpt; | |
} | |
String.prototype.repeatJsCore = function repeat(count){ | |
var str = this | |
, res = '' | |
, n = count; | |
if(n < 0 || n == Infinity)throw RangeError("Count can't be negative"); | |
for(;n > 0; (n >>>= 1) && (str += str))if(n & 1)res += str; | |
return res; | |
}; | |
}; | |
suite.add("var str = '0'.repeat(n);", function () { | |
var str = '0'.repeat(n); | |
}); | |
suite.add("var i = 0, str = '';", function () { | |
var i = 0, str = ''; | |
for (; i < n; str += '0', i++); | |
}); | |
suite.add("var i = 0, str = '';", function () { | |
var i = 0, str = ''; | |
n%2 ? (i = 1, str += '0', str) : str; | |
for (; i < n; str += '00', i+=2); | |
}); | |
suite.add("var str = '0'.repeatPolyfill(n);", function () { | |
var str = '0'.repeatPolyfill(n); | |
}); | |
suite.add("var n2 = Math.floor(n/2), i = 0;", function () { | |
var n2 = Math.floor(n/2), i = 0; | |
arr = new Array(n2); | |
n2%2 ? (i = 1, arr[0] = 0, arr) : arr | |
for (; i < n2; arr[i++] = 0, arr[i++] = 0); | |
var str = arr.join('0') + (n-(2*n2) ? '00' : '0') | |
}); | |
suite.add("var n2 = Math.floor(n/2);", function () { | |
var n2 = Math.floor(n/2); | |
var i = 0, str = ''; | |
n2%2 ? (i = 1, str += '0', str) : str; | |
for (; i < n2; str += '00', i+=2); | |
str = str + str + (n%2 ? '0' : ''); | |
}); | |
suite.add("var str = '0'.repeatJsCore(n);", function () { | |
var str = '0'.repeatJsCore(n); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Fastest way to fill a string #jsbench #jsperf"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment