set vs. map vs. object in various loops (v4)

Revision 4 of this benchmark created on


Description

Setup

var theArr = Array.from({ length: 10000 }, (_, el) => el)
var theSet = new Set(theArr)
var theObject = Object.assign({}, ...theArr.map(num => ({ [num]: true })))
var theMap = new Map(theArr.map(num => [num, true]))

var theTarget = 9000


// Array

function isTargetThereFor(arr, target) {
  const len = arr.length
  for (let i = 0; i < len; i++) {
    if (arr[i] === target) {
      return true
    }
  }
  return false
}
function isTargetThereForReverse(arr, target) {
  const len = arr.length
  for (let i = len; i > 0; i--) {
    if (arr[i] === target) {
      return true
    }
  }
  return false
}

function isTargetThereIncludes(arr, target) {
  return arr.includes(target)
}

// Set

function isTargetThereSet(numberSet, target) {
  return numberSet.has(target)
}

// Object 

function isTargetThereHasOwnProperty(obj, target) {
  return obj.hasOwnProperty(target)
}
function isTargetThereIn(obj, target) {
  return target in obj
}
function isTargetThereSelectKey(obj, target) {
  return obj[target]
}

// Map

function isTargetThereMap(numberMap, target) {
  return numberMap.has(target)
}

Test runner

Ready to run.

Testing in
TestOps/sec
for-loop
isTargetThereFor(theArr, theTarget);
ready
for-loop reversed
isTargetThereForReverse(theArr, theTarget);
ready
array.includes
isTargetThereIncludes(theArr, theTarget);
ready
set.has
isTargetThereSet(theSet, theTarget);
ready
obj.hasOwnProperty
isTargetThereHasOwnProperty(theObject, theTarget);
ready
for-in-loop
isTargetThereIn(theObject, theTarget);
ready
obj[key]
isTargetThereSelectKey (theObject, theTarget);
ready
map.has
isTargetThereMap(theMap, theTarget);
ready
asdfqwefwef
isTargetThereMap(theMap, theTarget);
ready

Revisions

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