Last active
November 26, 2015 19:07
-
-
Save lerouxb/ca82699f2e70495789a8 to your computer and use it in GitHub Desktop.
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
function a(input) { | |
var index = []; | |
var item; | |
for (var i=0; i<input.length; i++) { | |
item = input[i]; | |
if (index.indexOf(item) === -1) { | |
index.push(item); | |
} | |
} | |
return index; | |
} | |
function b(input) { | |
var index = {}; | |
var item; | |
for (var i=0; i<input.length; i++) { | |
item = input[i]; | |
if (index[item] === undefined) { | |
index[item] = true; | |
} | |
} | |
return index; | |
} | |
function c(input) { | |
var index = {}; | |
var item; | |
for (var i=0; i<input.length; i++) { | |
item = input[i]; | |
index[item] = true; | |
} | |
return index; | |
} | |
function test(f, input, times) { | |
for (var i=0; i<times; i++) { | |
f(input()); | |
} | |
} | |
function first(amount) { | |
return function() { | |
var data = []; | |
for (var i=0; i<amount; i++) { | |
data.push(i); | |
} | |
return data; | |
} | |
} | |
function random(amount, range) { | |
return function() { | |
var data = []; | |
for (var i=0; i<amount; i++) { | |
data.push(Math.floor(Math.random()*range)); | |
} | |
return data; | |
} | |
} | |
var testAmount = 1000; | |
var testRange = 100; | |
var testTimes = 10000; | |
console.log('amount, range, times'); | |
console.log(testAmount+', '+testRange+', '+testTimes); | |
console.log('--------------------') | |
console.time('a'); test(a, random(testAmount, testRange), testTimes); console.timeEnd('a'); | |
console.time('b'); test(b, random(testAmount, testRange), testTimes); console.timeEnd('b'); | |
console.time('c'); test(c, random(testAmount, testRange), testTimes); console.timeEnd('c'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment