Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Last active March 7, 2022 20:16
Show Gist options
  • Save snewcomer/ac7bffd8c79ddedf54df107bf4a64399 to your computer and use it in GitHub Desktop.
Save snewcomer/ac7bffd8c79ddedf54df107bf4a64399 to your computer and use it in GitHub Desktop.
array.includes vs set.has
const makeArray = (size) => [...Array(size)].map(() => size);
const small = makeArray(1000);
const medium = makeArray(10000000);
const large = makeArray(100000000);
const solution1 = (arr) => {
arr.includes(arr.length - 1);
};
const solution2 = (arr) => {
const set = new Set(arr);
set.has(arr.length - 1);
};
let t1 = process.hrtime();
solution1(small);
let t2 = process.hrtime(t1);
console.info('Small: Array.includes execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
t1 = process.hrtime();
solution2(small);
t2 = process.hrtime(t1);
console.info('Small: Set.has execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
t1 = process.hrtime();
solution1(medium);
t2 = process.hrtime(t1);
console.info('Medium: Array.includes execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
t1 = process.hrtime();
solution2(medium);
t2 = process.hrtime(t1);
console.info('Medium: Set.has execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
t1 = process.hrtime();
solution1(large);
t2 = process.hrtime(t1);
console.info('Large: Array.includes execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
t1 = process.hrtime();
solution2(large);
t2 = process.hrtime(t1);
console.info('Large: Set.has execution time (hr): %ds %dms', t2[0], t2[1] / 1000000);
@snewcomer
Copy link
Author

snewcomer commented Mar 7, 2022

const makeArray = (size) => [...Array(size)].map(() => size);

const small =  makeArray(1000);
const medium = makeArray(10000000);
const large =  makeArray(100000000);

const solution1 = arr => {
  arr.includes(arr.length - 1)
}

const solution2 = arr => {
  const set = new Set(arr)
  set.has(arr.length - 1)
}

let t1 = performance.now();
solution1(small);
let t2 = performance.now();
console.warn(`Small includes: ${t2 - t1}ms`);

t1 = performance.now();
solution2(small);
t2 = performance.now();
console.warn(`Small Set.has: ${t2 - t1}ms`);

t1 = performance.now();
solution1(medium);
t2 = performance.now();
console.warn(`Medium includes: ${t2 - t1}ms`);

t1 = performance.now();
solution2(medium);
t2 = performance.now();
console.warn(`Medium Set.has: ${t2 - t1}ms`);

t1 = performance.now();
solution1(large);
t2 = performance.now();
console.warn(`Large includes: ${t2 - t1}ms`);

t1 = performance.now();
solution2(large);
t2 = performance.now();
console.warn(`Large Set.has: ${t2 - t1}ms`);

@snewcomer
Copy link
Author

snewcomer commented Mar 7, 2022

Without Set initialization

const makeArray = (size) => [...Array(size)].map(() => size);

const small =  makeArray(10000);
const medium = makeArray(10000000);
const large =  makeArray(100000000);

const solution1 = (arr, type) => {
  let t1 = performance.now();
  arr.includes(arr.length - 1)
  let t2 = performance.now();
  console.warn(`${type}: Array.includes: ${t2 - t1}ms`);
}

const solution2 = (arr, type) => {
  const set = new Set(arr)
  let t1 = performance.now();
  set.has(arr.length-1)
  let t2 = performance.now();
  console.warn(`${type}: Set.has: ${t2 - t1}ms`);
}

solution1(small, 'Small');
solution2(small, 'Small');

solution1(medium, 'Medium');
solution2(medium, 'Medium');

solution1(large, 'Large');
solution2(large, 'Large');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment