Created
January 30, 2021 14:08
-
-
Save ForbesLindesay/cb5d6ea7720ee8912109dd2cca787590 to your computer and use it in GitHub Desktop.
Array vs. Double Array for Queue
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 testSimpleArray(size) { | |
const results = []; | |
let queue = []; | |
for (let run = 0; run < 10000; run++) { | |
for (let i = 0; i < size; i++) { | |
queue.push(i); | |
} | |
for (let i = 0; i < size; i++) { | |
results.push(queue.shift()); | |
} | |
} | |
return results; | |
} | |
function testDoubleArray(size) { | |
const results = []; | |
let queue = [[], []]; | |
for (let run = 0; run < 100; run++) { | |
for (let i = 0; i < size; i++) { | |
queue[1].push(i); | |
} | |
for (let i = 0; i < size; i++) { | |
if (!queue[0].length) { | |
queue = [queue[1].reverse(), queue[0]]; | |
} | |
results.push(queue[0].pop()); | |
} | |
} | |
return results; | |
} | |
for (let size = 1; size < 100000; size *= 10) { | |
// warmup | |
for (let i = 0; i < 2; i++) { | |
testSimpleArray(size); | |
testDoubleArray(size); | |
} | |
console.time(`double array - ${size}`); | |
testDoubleArray(size); | |
console.timeEnd(`double array - ${size}`); | |
console.time(`simple array - ${size}`); | |
testSimpleArray(size); | |
console.timeEnd(`simple array - ${size}`); | |
console.time(`double array - ${size}`); | |
testDoubleArray(size); | |
console.timeEnd(`double array - ${size}`); | |
console.time(`simple array - ${size}`); | |
testSimpleArray(size); | |
console.timeEnd(`simple array - ${size}`); | |
} |
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
double array - 1: 0.137ms | |
simple array - 1: 2.688ms | |
double array - 1: 0.069ms | |
simple array - 1: 0.302ms | |
double array - 10: 0.172ms | |
simple array - 10: 3.214ms | |
double array - 10: 0.172ms | |
simple array - 10: 3.282ms | |
double array - 100: 0.204ms | |
simple array - 100: 50.374ms | |
double array - 100: 0.431ms | |
simple array - 100: 50.953ms | |
double array - 1000: 7.456ms | |
simple array - 1000: 852.335ms | |
double array - 1000: 15.006ms | |
simple array - 1000: 800.256ms | |
double array - 10000: 28.426ms | |
simple array - 10000: 7.543s | |
double array - 10000: 28.676ms | |
simple array - 10000: 7.704s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment