Skip to content

Instantly share code, notes, and snippets.

@charlenopires
Created May 18, 2017 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlenopires/cf810d101e2e71899e854d6a172e5282 to your computer and use it in GitHub Desktop.
Save charlenopires/cf810d101e2e71899e854d6a172e5282 to your computer and use it in GitHub Desktop.
Perfs2Recursion
<input type="button" onclick="run()" value="Start" />
<div id="output" style="white-space:pre-wrap;">
</div>
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var tbody = document.createElement('tbody');
for (var i = 0; i < 100; i++) {
tbody.insertRow().insertCell()
}
var rows = tbody.rows;
var output = document.getElementById("output");
function log(text) {
output.appendChild(document.createTextNode(text + '\n'));
}
function run() {
log("Starting...");
suite.run({ 'async': true });
}
// add tests
suite
.add('for push', function() {
var arr = [];
for (var index = 0, count = rows.length; index < count; ++index) {
arr.push(rows[index]);
}
})
.add('for set', function() {
var arr = Array(rows.length);
for (var index = 0, count = rows.length; index < count; ++index) {
arr[index] = rows[index];
}
})
.add('push.apply', function() {
var arr = [];
[].push.apply(arr, rows);
})
.add('slice.call', function() {
//will fail in IE8
var arr = [].slice.call(rows);
})
.add('while set', function() {
var arr = Array(rows.length);
var index = rows.length;
while (index--) {
arr[index] = rows[index];
}
})
// add listeners
.on('cycle', function(event) {
log(String(event.target));
})
.on('complete', function() {
log('Fastest is ' + this.filter('fastest').map('name'));
})
// run tests async
//.run({ 'async': true })
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment