Create array of one member vs using Set

Benchmark created on


Description

This tests whether creating an array of one member by testing first using .isArray() and then wrapping the member in an array is faster or slower than creating a single member array using the spread command on a Set object.

Preparation HTML

<script>

function createArray1(item1) {

  let itemArray1 = Array.isArray(item1) ? item1 : [item1];

  console.log(itemArray1);
}

function createArray2(item2) {
  let itemArray2 = new Array(...new Set(item2));

  console.log(itemArray2);
}

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Build Array by checking
createArray1('a thing');
ready
Build Array from Set
createArray2('a thing')
ready

Revisions

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