Skip to content

Instantly share code, notes, and snippets.

@Constellation
Created January 5, 2016 16:01
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 Constellation/8db5f5b8f12fe7e283d0 to your computer and use it in GitHub Desktop.
Save Constellation/8db5f5b8f12fe7e283d0 to your computer and use it in GitHub Desktop.
/*
Copyright (C) 2016 Yusuke Suzuki <utatane.tea@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var global = this;
Benchmark.prototype.setup = `
function createLinkedList(count) {
var node = new LinkedListNode(0);
var list = {length: count, head: node};
for (var i = 0; i < count; i++) {
node.next = new LinkedListNode(i);
node.next.prev = node;
node = node.next;
}
return list;
}
function LinkedListNode(value) {
this.prev = null;
this.next = null;
this.value = value;
}
function createArray(count) {
var array = [];
for (var i = 0; i < count; i++) {
array[i] = i;
}
return array;
}
function createSet(count) {
var set = new Set;
for (var i = 0; i < count; i++) {
set.add(i);
}
return set;
}
var COUNT = 2000;
var list = createLinkedList(COUNT);
var array = createArray(COUNT);
var set = createSet(COUNT);
global.lastSum = 0;
`;
Benchmark.prototype.teardown = `
global.lastSum = 0;
`;
Benchmark.options.minSamples = 100;
var suite = new Benchmark.Suite();
suite
.add('Linked List', `
var sum = 0;
var node = list.head; for (var i = 0; i < list.length; i++) {
node = node.next;
sum += node.value;
}
global.lastSum = sum;
`)
.add('Array', `
var sum = 0;
for (var i = 0; i < array.length; i++) {
sum += array[i];
}
global.lastSum = sum;
`)
.add('Array forEach', `
var sum = 0;
array.forEach(function(item) {
sum += item;
});
global.lastSum = sum;
`)
.add('Array for-of', `
var sum = 0;
for (var item of array) {
sum += item;
}
global.lastSum = sum;
`)
.add('Set forEach', `
var sum = 0;
set.forEach(function(item) {
sum += item;
});
global.lastSum = sum;
`)
.add('Set for-of', `
var sum = 0;
for (var item of set) {
sum += item;
}
global.lastSum = sum;
`)
.on('cycle', function(event) {
print(String(event.target));
})
.on('complete', function() {
print(`Fastest is ${this.filter('fastest').map('name')}`);
})
.run({
async: false,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment