Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
shuffle array [dsng-manscaped]
(version: 1)
Comparing performance of:
map sort map vs Fisher-Yates
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
map sort map
const arr = Array.from({length: 10000}); function shuffle(arr) { return arr .map((name) => ({ name, randomValue: Math.random() })) .sort((a, b) => a.randomValue - b.randomValue) .map(({ name }) => name); } shuffle(arr)
Fisher-Yates
const arr = Array.from({length: 10000}); function shuffle(array) { let currentIndex = array.length; let randomIndex; while (currentIndex != 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; } shuffle(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map sort map
Fisher-Yates
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!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark is designed to measure the performance of JavaScript's array shuffling algorithms. Specifically, it tests two approaches: 1. **Using `Array.prototype.sort()` with a custom comparison function**: This approach uses the built-in `sort()` method and provides a custom comparison function to sort the array elements based on their random values. 2. **Fisher-Yates shuffle algorithm**: This is an in-place shuffling algorithm that rearranges the elements of the array without using additional data structures. **Comparison Options** The benchmark compares these two approaches, providing insight into which one performs better under different conditions. **Pros and Cons** * **Using `Array.prototype.sort()` with a custom comparison function**: + Pros: Simple to implement, leverages built-in functionality. + Cons: Sorts the array in ascending order, which may not be desirable for all use cases; can be slower than other algorithms due to its inherent O(n log n) complexity. * **Fisher-Yates shuffle algorithm**: + Pros: Efficient, with a time complexity of O(n); rearranges elements in-place, avoiding additional data structures; suitable for most shuffling needs. + Cons: Requires more code and manual implementation effort. **Library Usage** In the benchmark, the `Array.from()` method is used to create an array from a specified length. This method is a part of the ECMAScript Standard Library and provides a convenient way to create arrays without using the traditional `new Array()` constructor. **Special JavaScript Feature/Syntax** There are no specific JavaScript features or syntaxes mentioned in this benchmark. However, it's worth noting that the use of template literals (e.g., `"map sort map"`) is a feature introduced in ECMAScript 2015 (ES6) for creating string templates. **Alternative Approaches** Other array shuffling algorithms can be used as alternatives to the Fisher-Yates algorithm: * **Randomized shuffle using `Math.random()`**: This approach involves generating random indices and swapping elements based on those indices. * **Lomuto shuffle**: A variation of the Fisher-Yates algorithm that is slightly faster and more efficient. Keep in mind that these alternative algorithms may have different performance characteristics, and the choice of implementation depends on specific requirements and use cases.
Related benchmarks:
LIS-test2
Object, Map: string vs numeric smi keys access performance
set.has vs. array.includes vs obj[key] vs map.get 2
Array Find vs Some (shuffled array) 2
Comments
Confirm delete:
Do you really want to delete benchmark?