Skip to content

Instantly share code, notes, and snippets.

@cevek
Last active October 25, 2018 17:29
Show Gist options
  • Save cevek/771b7351140c770e1d23ebd195912435 to your computer and use it in GitHub Desktop.
Save cevek/771b7351140c770e1d23ebd195912435 to your computer and use it in GitHub Desktop.
set vs array performance
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
const perfMap = {};
function perf(row, col, fn) {
const start = Date.now();
const res = fn();
if (!perfMap[row]) perfMap[row] = {};
perfMap[row][col] = Date.now() - start;
return res;
}
function printPerf() {
console.table(perfMap);
}
function main() {
const listObj = [{x: 1}, {y: 2}, {z: 3}, {d: 4}, {e: 5}];
for (let i = 0; i < 1000; i++) {
listObj.push({a: i});
}
const list10 = listObj.slice(0, 10);
const list20 = listObj.slice(0, 20);
const list100 = listObj.slice(0, 100);
const list1000 = listObj.slice(0, 1000);
let listIdx = 0;
function getObj(list) {
return list[listIdx++ % list.length];
}
function setHas(list) {
const set = new Set(list);
for (let i = 0; i < 1e6; i++) {
var x = i === -1 ? 1 : set.has(getObj(list));
}
return x;
}
function indexOf(list) {
for (let i = 0; i < 1e6; i++) {
var x = i === -1 ? 1 : list.indexOf(getObj(list));
}
return x;
}
function _customIndexOfArray(arr, val) {
let pos = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === val) {
pos = i;
break;
}
}
return pos;
}
function customIndexOf(list) {
for (let i = 0; i < 1e6; i++) {
var x = i === -1 ? 1 : _customIndexOfArray(list, getObj(list));
}
return x;
}
function arrayFor(list) {
for (let i = 0; i < 1e6; i++) {
for (let j = 0; j < list.length; j++) {
var x = i === -1 ? 1 : list[j];
}
}
return x;
}
function _setForEachIter(key, value) {
return value === getObj(listObj);
}
function setForEach(list) {
const set = new Set(list);
for (let i = 0; i < 1e6; i++) {
var x = i === -1 ? 1 : set.forEach(_setForEachIter);
}
return x;
}
function setForOf(list) {
const set = new Set(list);
for (let i = 0; i < 1e6; i++) {
for (const value of set) {
var x = i === -1 ? 1 : value === getObj(list);
}
}
return x;
}
function createSetAndAdd(count) {
for (let i = 0; i < 1e6; i++) {
const set = new Set();
for (let j = 0; j < count; j++) {
set.add(getObj(listObj));
}
var x = set;
}
return x;
}
function createArrayAndUniquePush(count) {
for (let i = 0; i < 1e6; i++) {
const array = [];
for (let j = 0; j < count; j++) {
const obj = getObj(listObj);
if (array.indexOf(obj) === -1) array.push(obj);
}
var x = array;
}
return x;
}
function createArrayAndPush(count) {
for (let i = 0; i < 1e6; i++) {
const array = [];
for (let j = 0; j < count; j++) {
const obj = getObj(listObj);
array.push(obj);
}
var x = array;
}
return x;
}
function createArray(arr) {
for (let i = 0; i < 1e6; i++) {
const array = arr.slice();
var x = array[array.length / 2 | 0];
}
return x;
}
function createSet(arr) {
for (let i = 0; i < 1e6; i++) {
const set = new Set(arr);
var obj = arr[arr.length / 2 | 0];
var x = set;
}
return x;
}
function remove1ItemArraySplice(arr) {
for (let i = 0; i < 1e6; i++) {
const array = arr.slice();
const obj = array[array.length / 2 | 0];
const pos = array.indexOf(obj);
if (pos > -1) array.splice(pos, 1);
var x = array;
}
return x;
}
function remove1ItemArrayFast(arr) {
for (let i = 0; i < 1e6; i++) {
const array = arr.slice();
const obj = array[array.length / 2 | 0];
for (let j = 0; j < array.length; j++) {
if (obj === array[j]) {
array[j] = array[array.length - 1];
array.pop();
break;
}
}
var x = array;
}
return x;
}
function remove1ItemSet(arr) {
for (let i = 0; i < 1e6; i++) {
const set = new Set(arr);
const obj = arr[arr.length / 2 | 0];
set.delete(obj);
var x = set;
}
return x;
}
perf("set: has", 10, () => setHas(list10));
perf("set: has", 20, () => setHas(list20));
perf("set: has", 100, () => setHas(list100));
perf("set: has", 1000, () => setHas(list1000));
perf("array: indexOf", 10, () => indexOf(list10));
perf("array: indexOf", 20, () => indexOf(list20));
perf("array: indexOf", 100, () => indexOf(list100));
perf("array: indexOf", 1000, () => indexOf(list1000));
perf("array: customIndexOf", 10, () => customIndexOf(list10));
perf("array: customIndexOf", 20, () => customIndexOf(list20));
perf("array: customIndexOf", 100, () => customIndexOf(list100));
perf("array: customIndexOf", 1000, () => customIndexOf(list1000));
perf("array: for", 10, () => arrayFor(list10));
perf("array: for", 20, () => arrayFor(list20));
perf("array: for", 100, () => arrayFor(list100));
perf("array: for", 1000, () => arrayFor(list1000));
perf("set: forEach", 10, () => setForEach(list10));
perf("set: forEach", 20, () => setForEach(list20));
perf("set: forEach", 100, () => setForEach(list100));
perf("set: forEach", 1000, () => setForEach(list1000));
perf("set: for of", 10, () => setForOf(list10));
perf("set: for of", 20, () => setForOf(list20));
perf("set: for of", 100, () => setForOf(list100));
perf("set: for of", 1000, () => setForOf(list1000));
perf("set: create + add", 10, () => createSetAndAdd(10));
perf("set: create + add", 20, () => createSetAndAdd(20));
perf("set: create + add", 100, () => createSetAndAdd(100));
perf("array: create + unique push", 10, () => createArrayAndUniquePush(10));
perf("array: create + unique push", 20, () => createArrayAndUniquePush(20));
perf("array: create + unique push", 100, () => createArrayAndUniquePush(100));
perf("array: create + push", 10, () => createArrayAndPush(10));
perf("array: create + push", 20, () => createArrayAndPush(20));
perf("array: create + push", 100, () => createArrayAndPush(100));
perf("array: remove 1", 10, () => remove1ItemArraySplice(list10));
perf("array: remove 1", 20, () => remove1ItemArraySplice(list20));
perf("array: remove 1", 100, () => remove1ItemArraySplice(list100));
perf("array: remove 1 fast", 10, () => remove1ItemArrayFast(list10));
perf("array: remove 1 fast", 20, () => remove1ItemArrayFast(list20));
perf("array: remove 1 fast", 100, () => remove1ItemArrayFast(list100));
perf("array: create", 10, () => createArray(list10));
perf("array: create", 20, () => createArray(list20));
perf("array: create", 100, () => createArray(list100));
perf("set: create", 10, () => createSet(list10));
perf("set: create", 20, () => createSet(list20));
perf("set: create", 100, () => createSet(list100));
perf("set: delete 1", 10, () => remove1ItemSet(list10));
perf("set: delete 1", 20, () => remove1ItemSet(list20));
perf("set: delete 1", 100, () => remove1ItemSet(list100));
printPerf();
}
main();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment