Last active
January 3, 2021 00:38
-
-
Save tomchen/25b11e8d41025071bb34012dad89bf22 to your computer and use it in GitHub Desktop.
JavaScript /Node.js `Map`, `Object`, `Array`'s `set`, `has`, `get`, `delete` methods performance / speed comparison
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
/* | |
Below is the result for the method 'set' of Map/Object/Array, when both keys and values are from 0 to 9999. | |
Method 'set' of Map x 3,983 ops/sec ±0.21% (94 runs sampled) | |
Method 'set' of Object x 6,265 ops/sec ±0.31% (96 runs sampled) | |
Method 'set' of Array x 35,736 ops/sec ±0.37% (93 runs sampled) | |
Fastest is Method 'set' of Array | |
Below is the result for the method 'has' (or similar) of Map/Object/Array, when both keys and values are from 0 to 9999. | |
Method 'has' of Map x 6,244 ops/sec ±1.33% (94 runs sampled) | |
Method 'in' of Object x 63,996 ops/sec ±0.27% (95 runs sampled) | |
Method 'check undefined' of Object x 51,230 ops/sec ±1.04% (96 runs sampled) | |
Method 'has' of Object x 10,946 ops/sec ±0.07% (95 runs sampled) | |
Method 'in' of Array x 77,155 ops/sec ±0.19% (96 runs sampled) | |
Method 'check undefined' of Array x 95,343 ops/sec ±1.40% (93 runs sampled) | |
Fastest is Method 'check undefined' of Array | |
Below is the result for the method 'get' of Map/Object/Array, when both keys and values are from 0 to 9999. | |
Method 'get' of Map x 5,743 ops/sec ±0.04% (97 runs sampled) | |
Method 'get' of Object x 51,087 ops/sec ±0.93% (95 runs sampled) | |
Method 'get' of Array x 84,954 ops/sec ±0.34% (95 runs sampled) | |
Fastest is Method 'get' of Array | |
Below is the result for the method 'delete' (or similar) of Map/Object/Array, when both keys and values are from 0 to 9999. | |
Method 'delete' of Map x 10,385 ops/sec ±1.37% (92 runs sampled) | |
Method 'delete' of Object x 842 ops/sec ±0.53% (93 runs sampled) | |
Method 'delete' of Array x 836 ops/sec ±1.17% (93 runs sampled) | |
Method 'set undefined' of Object x 144,662 ops/sec ±0.76% (94 runs sampled) | |
Method 'set undefined' of Array x 145,067 ops/sec ±0.53% (96 runs sampled) | |
Fastest is Method 'set undefined' of Array | |
Below is the result for the method 'set' of Map/Object/Array, when both keys and values are random numbers from 0 to 100000. | |
Method 'set' of Map x 3,775 ops/sec ±0.20% (96 runs sampled) | |
Method 'set' of Object x 572 ops/sec ±0.07% (93 runs sampled) | |
Method 'set' of Array x 541 ops/sec ±0.23% (91 runs sampled) | |
Fastest is Method 'set' of Map | |
Below is the result for the method 'has' (or similar) of Map/Object/Array, when both keys and values are random numbers from 0 to 100000. | |
Method 'has' of Map x 5,593 ops/sec ±0.74% (94 runs sampled) | |
Method 'in' of Object x 2,153 ops/sec ±0.17% (96 runs sampled) | |
Method 'check undefined' of Object x 1,837 ops/sec ±1.04% (95 runs sampled) | |
Method 'has' of Object x 2,077 ops/sec ±0.22% (95 runs sampled) | |
Method 'in' of Array x 30,320 ops/sec ±1.81% (92 runs sampled) | |
Method 'check undefined' of Array x 24,014 ops/sec ±0.70% (93 runs sampled) | |
Fastest is Method 'in' of Array | |
Below is the result for the method 'get' of Map/Object/Array, when both keys and values are random numbers from 0 to 100000. | |
Method 'get' of Map x 5,131 ops/sec ±1.32% (95 runs sampled) | |
Method 'get' of Object x 1,836 ops/sec ±0.18% (95 runs sampled) | |
Method 'get' of Array x 21,725 ops/sec ±0.75% (96 runs sampled) | |
Fastest is Method 'get' of Array | |
Below is the result for the method 'delete' (or similar) of Map/Object/Array, when both keys and values are random numbers from 0 to 100000. | |
Method 'delete' of Map x 9,566 ops/sec ±1.36% (94 runs sampled) | |
Method 'delete' of Object x 834 ops/sec ±0.05% (94 runs sampled) | |
Method 'delete' of Array x 1,366 ops/sec ±1.28% (93 runs sampled) | |
Method 'set undefined' of Object x 567 ops/sec ±0.37% (92 runs sampled) | |
Method 'set undefined' of Array x 5,628 ops/sec ±0.25% (94 runs sampled) | |
Fastest is Method 'delete' of Map | |
Below is the result for the method 'set' of Map/Object, when both keys and values are random characters whose codepoints are from 0 to 100000. | |
Method 'set' of Map x 2,514 ops/sec ±0.48% (96 runs sampled) | |
Method 'set' of Object x 2,930 ops/sec ±0.31% (93 runs sampled) | |
Fastest is Method 'set' of Object | |
Below is the result for the method 'has' (or similar) of Map/Object, when both keys and values are random characters whose codepoints are from 0 to 100000. | |
Method 'has' of Map x 3,377 ops/sec ±0.05% (96 runs sampled) | |
Method 'in' of Object x 4,745 ops/sec ±0.21% (96 runs sampled) | |
Method 'check undefined' of Object x 3,531 ops/sec ±0.41% (96 runs sampled) | |
Method 'has' of Object x 2,467 ops/sec ±0.63% (93 runs sampled) | |
Fastest is Method 'in' of Object | |
Below is the result for the method 'get' of Map/Object, when both keys and values are random characters whose codepoints are from 0 to 100000. | |
Method 'get' of Map x 3,323 ops/sec ±0.25% (95 runs sampled) | |
Method 'get' of Object x 3,436 ops/sec ±0.22% (96 runs sampled) | |
Fastest is Method 'get' of Object | |
Below is the result for the method 'delete' (or similar) of Map/Object, when both keys and values are random characters whose codepoints are from 0 to 100000. | |
Method 'delete' of Map x 10,606 ops/sec ±0.72% (93 runs sampled) | |
Method 'delete' of Object x 3,333 ops/sec ±0.91% (94 runs sampled) | |
Method 'set undefined' of Object x 2,913 ops/sec ±0.74% (96 runs sampled) | |
Fastest is Method 'delete' of Map | |
*/ | |
const Benchmark = require("benchmark"); | |
const n = 10000; | |
const ran = 100000; | |
let temp; | |
let obj = {}; | |
let obj2 = {}; | |
let map = new Map(); | |
let arr = []; | |
let arr2 = []; | |
let suite; | |
function getRandomInt(max) { | |
return Math.floor(Math.random() * Math.floor(max)); | |
} | |
const arrayRandomNumbers = []; | |
const arrayRandomChars = []; | |
let int; | |
for (let i = 0; i < n; i++) { | |
int = getRandomInt(ran); | |
arrayRandomNumbers.push(int); | |
arrayRandomChars.push(String.fromCharCode(int)); | |
} | |
console.log( | |
`Below is the result for the method 'set' of Map/Object/Array, when both keys and values are from 0 to ${( | |
n - 1 | |
).toString()}.` | |
); | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'set' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
map.set(i, i); | |
} | |
}) | |
.add("Method 'set' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj[i] = i; | |
} | |
}) | |
.add("Method 'set' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
arr[i] = i; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'has' (or similar) of Map/Object/Array, when both keys and values are from 0 to ${( | |
n - 1 | |
).toString()}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
arr = []; | |
arr2 = []; | |
for (let i = 0; i < n; i++) { | |
obj[i] = i; | |
obj2[i] = i; | |
map.set(i, i); | |
arr.push(i); | |
arr2.push(i); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'has' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.has(i); | |
} | |
}) | |
.add("Method 'in' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = i in obj; | |
} | |
}) | |
.add("Method 'check undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2[i] === undefined; | |
} | |
}) | |
.add("Method 'has' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2.hasOwnProperty(i); | |
} | |
}) | |
.add("Method 'in' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = i in arr; | |
} | |
}) | |
.add("Method 'check undefined' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arr2[i] === undefined; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'get' of Map/Object/Array, when both keys and values are from 0 to ${( | |
n - 1 | |
).toString()}.` | |
); | |
obj = {}; | |
map = new Map(); | |
arr = []; | |
for (let i = 0; i < n; i++) { | |
obj[i] = i; | |
map.set(i, i); | |
arr.push(i); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'get' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.get(i); | |
} | |
}) | |
.add("Method 'get' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj[i]; | |
} | |
}) | |
.add("Method 'get' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arr[i]; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'delete' (or similar) of Map/Object/Array, when both keys and values are from 0 to ${( | |
n - 1 | |
).toString()}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
arr = []; | |
arr2 = []; | |
for (let i = 0; i < n; i++) { | |
obj[i] = i; | |
obj2[i] = i; | |
map.set(i, i); | |
arr.push(i); | |
arr2.push(i); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'delete' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.delete(i); | |
} | |
}) | |
.add("Method 'delete' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
delete obj[i]; | |
} | |
}) | |
.add("Method 'delete' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
delete arr[i]; | |
} | |
}) | |
.add("Method 'set undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj2[i] = undefined; | |
} | |
}) | |
.add("Method 'set undefined' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
arr2[i] = undefined; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'set' of Map/Object/Array, when both keys and values are random numbers from 0 to ${ran}.` | |
); | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'set' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
map.set(arrayRandomNumbers[i], arrayRandomNumbers[i]); | |
} | |
}) | |
.add("Method 'set' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
} | |
}) | |
.add("Method 'set' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
arr[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'has' (or similar) of Map/Object/Array, when both keys and values are random numbers from 0 to ${ran}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
arr = []; | |
arr2 = []; | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
obj2[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
map.set(arrayRandomNumbers[i], arrayRandomNumbers[i]); | |
arr.push(arrayRandomNumbers[i]); | |
arr2.push(arrayRandomNumbers[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'has' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.has(arrayRandomNumbers[i]); | |
} | |
}) | |
.add("Method 'in' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arrayRandomNumbers[i] in obj; | |
} | |
}) | |
.add("Method 'check undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2[arrayRandomNumbers[i]] === undefined; | |
} | |
}) | |
.add("Method 'has' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2.hasOwnProperty(arrayRandomNumbers[i]); | |
} | |
}) | |
.add("Method 'in' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arrayRandomNumbers[i] in arr; | |
} | |
}) | |
.add("Method 'check undefined' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arr2[arrayRandomNumbers[i]] === undefined; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'get' of Map/Object/Array, when both keys and values are random numbers from 0 to ${ran}.` | |
); | |
obj = {}; | |
map = new Map(); | |
arr = []; | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
map.set(arrayRandomNumbers[i], arrayRandomNumbers[i]); | |
arr.push(arrayRandomNumbers[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'get' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.get(arrayRandomNumbers[i]); | |
} | |
}) | |
.add("Method 'get' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj[arrayRandomNumbers[i]]; | |
} | |
}) | |
.add("Method 'get' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arr[arrayRandomNumbers[i]]; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'delete' (or similar) of Map/Object/Array, when both keys and values are random numbers from 0 to ${ran}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
arr = []; | |
arr2 = []; | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
obj2[arrayRandomNumbers[i]] = arrayRandomNumbers[i]; | |
map.set(arrayRandomNumbers[i], arrayRandomNumbers[i]); | |
arr.push(arrayRandomNumbers[i]); | |
arr2.push(arrayRandomNumbers[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'delete' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.delete(arrayRandomNumbers[i]); | |
} | |
}) | |
.add("Method 'delete' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
delete obj[arrayRandomNumbers[i]]; | |
} | |
}) | |
.add("Method 'delete' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
delete arr[arrayRandomNumbers[i]]; | |
} | |
}) | |
.add("Method 'set undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj2[arrayRandomNumbers[i]] = undefined; | |
} | |
}) | |
.add("Method 'set undefined' of Array", function () { | |
for (let i = 0; i < n; i++) { | |
arr2[arrayRandomNumbers[i]] = undefined; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'set' of Map/Object, when both keys and values are random characters whose codepoints are from 0 to ${ran}.` | |
); | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'set' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
map.set(arrayRandomChars[i], arrayRandomChars[i]); | |
} | |
}) | |
.add("Method 'set' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomChars[i]] = arrayRandomChars[i]; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'has' (or similar) of Map/Object, when both keys and values are random characters whose codepoints are from 0 to ${ran}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomChars[i]] = arrayRandomChars[i]; | |
obj2[arrayRandomChars[i]] = arrayRandomChars[i]; | |
map.set(arrayRandomChars[i], arrayRandomChars[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'has' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.has(arrayRandomChars[i]); | |
} | |
}) | |
.add("Method 'in' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = arrayRandomChars[i] in obj; | |
} | |
}) | |
.add("Method 'check undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2[arrayRandomChars[i]] === undefined; | |
} | |
}) | |
.add("Method 'has' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj2.hasOwnProperty(arrayRandomChars[i]); | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'get' of Map/Object, when both keys and values are random characters whose codepoints are from 0 to ${ran}.` | |
); | |
obj = {}; | |
map = new Map(); | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomChars[i]] = arrayRandomChars[i]; | |
map.set(arrayRandomChars[i], arrayRandomChars[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'get' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.get(arrayRandomChars[i]); | |
} | |
}) | |
.add("Method 'get' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
temp = obj[arrayRandomChars[i]]; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); | |
console.log( | |
`Below is the result for the method 'delete' (or similar) of Map/Object, when both keys and values are random characters whose codepoints are from 0 to ${ran}.` | |
); | |
obj = {}; | |
obj2 = {}; | |
map = new Map(); | |
for (let i = 0; i < n; i++) { | |
obj[arrayRandomChars[i]] = arrayRandomChars[i]; | |
obj2[arrayRandomChars[i]] = arrayRandomChars[i]; | |
map.set(arrayRandomChars[i], arrayRandomChars[i]); | |
} | |
suite = new Benchmark.Suite(); | |
suite | |
.add("Method 'delete' of Map", function () { | |
for (let i = 0; i < n; i++) { | |
temp = map.delete(arrayRandomChars[i]); | |
} | |
}) | |
.add("Method 'delete' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
delete obj[arrayRandomChars[i]]; | |
} | |
}) | |
.add("Method 'set undefined' of Object", function () { | |
for (let i = 0; i < n; i++) { | |
obj2[arrayRandomChars[i]] = undefined; | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}); | |
suite.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment