Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find Smallest Number Not In An Array With Dynamic Max Value
(version: 0)
Comparing performance of:
Set With For Loop vs Sort With For Loop vs Sort With Reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; array.length = 10000; array.fill(undefined); array = array.map(() => Math.floor(Math.random() * array.length));
Tests:
Set With For Loop
const set = new Set(array); const maxSetValue = array.reduce(Math.max); function findSet() { for (let i = 0; i < maxSetValue; i++) if (!set.has(i)) return i; return -1; } findSet();
Sort With For Loop
const sortedArray = array.slice().sort((a, b) => a - b); const maxSortValue = sortedArray[array.length - 1]; function findSort() { for (let i = 0; i < maxSortValue; i++) if (!sortedArray.includes(i)) return i; return -1; } findSort();
Sort With Reduce
const sortedReduceArray = array.slice().sort((a, b) => a - b); function findReduce() { return sortedReduceArray.reduce((acc, current) => (Math.abs(current - acc) > 1 ? Math.min : Math.max)(current, acc), 0) + 1; } findReduce();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set With For Loop
Sort With For Loop
Sort With Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Array.Sort vs Math.Min-Max
Set.has v.s Array.includes
yoooooo
Set.has v.s Array.includes v2
Comments
Confirm delete:
Do you really want to delete benchmark?