Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find the fastest median
(version: 3)
Fastest array median
Comparing performance of:
Math.floor vs Bitwise ( >> ) vs Bitwise (~~)
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
Math.floor
const median = (columnId, leafRows) => { if (!leafRows.length) { return } const values = leafRows if (values.length === 1) { return values[0] } const mid = Math.floor(values.length / 2) const nums = values.sort((a, b) => a - b) return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2 } median('', [...Array(10_000_000).keys()])
Bitwise ( >> )
const median = (columnId, leafRows) => { if (!leafRows.length) { return } const values = leafRows if (values.length === 1) { return values[0] } let start = 0; let end = values.length; const mid = start + ((end - start) >> 1); const nums = values.sort((a, b) => a - b) return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2 } median('', [...Array(10_000_000).keys()])
Bitwise (~~)
const median = (columnId, leafRows) => { if (!leafRows.length) { return } const values = leafRows if (values.length === 1) { return values[0] } const mid = ~~(values.length / 2) const nums = values.sort((a, b) => a - b) return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2 } median('', [...Array(10_000_000).keys()])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.floor
Bitwise ( >> )
Bitwise (~~)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.floor
1.0 Ops/sec
Bitwise ( >> )
1.1 Ops/sec
Bitwise (~~)
1.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, tests the performance of different methods for calculating the median of an array of numbers in JavaScript. **Test Cases** There are three test cases: 1. **Math.floor**: This test case uses the built-in `Math.floor` function to calculate the median. 2. **Bitwise ( >> )**: This test case uses a bitwise shift operator (`>>`) to calculate the middle index of the array, and then accesses the corresponding element to get the median. 3. **Bitwise (~~)**: This test case uses the unary `~` operator (which is equivalent to `-1 << 31`) to calculate the negative index of the middle element, and then adds the next element if necessary. **Library Used** None. The tests only use built-in JavaScript functions and operators. **Special JS Feature/Syntax** * None mentioned in this explanation. **Options Compared** The three test cases compare the performance of different methods for calculating the median: * **Math.floor**: uses a straightforward but potentially slow approach, as it involves iterating over the entire array to sort it. * **Bitwise ( >> )**: uses a clever trick involving bitwise shift to calculate the middle index directly, without sorting the entire array. This approach is likely faster than `Math.floor`. * **Bitwise (~~)**: uses another clever trick involving the unary `~` operator to calculate the negative index of the middle element, and then adds the next element if necessary. The effectiveness of this approach is uncertain. **Pros and Cons** 1. **Math.floor**: * Pros: straightforward and easy to understand. * Cons: potentially slow due to sorting the entire array. 2. **Bitwise ( >> )**: * Pros: likely faster than `Math.floor` as it avoids sorting the entire array. * Cons: less intuitive and may be harder to understand for some developers. 3. **Bitwise (~~)**: * Pros: potentially interesting approach, but effectiveness uncertain. * Cons: unclear how well this approach performs in practice. **Other Alternatives** There might be other ways to calculate the median of an array in JavaScript, such as using `Array.prototype.reduce()` or a custom implementation. However, these alternatives are not tested in this benchmark, and their performance is likely similar to the methods presented here. It's worth noting that the **Bitwise ( >> )** approach is often referred to as "bit-twiddling" because of its use of bitwise operations. This technique can be useful in certain situations, but it may not always be the best choice for calculating medians.
Related benchmarks:
Sorting speed comparison
Array.sort() vs Math.min / Math.max 4 elements v2
Array.sort vs Math.min+Math.max (LONG ARRAYS)
Array.sort() vs Math.min / Math.max 4 elements vs d3 array
Javascript Array sorting performance with sort() and reduce() take2
Comments
Confirm delete:
Do you really want to delete benchmark?