Sets, objects or arrays - which one is faster?

Benchmark created on


Description

Different types of sets in JS - which one is the fastest? Tests whether Object.hasOwnProperty, Object in operator, Object[property], Set.has, Array.includes or Array.indexOf() > -1 is fastest.

Setup

const set = new Set();
const object = {};
const nullObject = Object.create(null);
const array = new Array();

Test runner

Ready to run.

Testing in
TestOps/sec
Add items to normal object
object[Math.random().toString()]=true
ready
Add items to null object
nullObject[Math.random().toString()]=true
ready
Add items to Set
set.add(Math.random().toString())
ready
Add items to array
array.push(Math.random().toString())
ready
Check if item in null object (property method)
nullObject[Math.random().toString()] == true
ready
Check if item in normal object (property method)
object[Math.random().toString()] == true
ready
Check if item in null object (in operator)
Math.random().toString() in nullObject
ready
Check if item in normal object (in operator)
Math.random().toString() in object
ready
Check if item in null object (hasOwnProperty on prototype)
Object.prototype.hasOwnProperty.call(nullObject, Math.random().toString())
ready
Check if item in normal object (hasOwnProperty on prototype)
Object.prototype.hasOwnProperty.call(object, Math.random().toString())
ready
Check if item in normal object (hasOwnProperty on instance)
object.hasOwnProperty(Math.random().toString())
ready
Check if item in Set
set.has(Math.random().toString())
ready
Check if item in array (Array.includes)
array.includes(Math.random().toString())
ready
Check if item in array (Array.indexOf)
array.indexOf(Math.random().toString()) > -1
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.