Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
min check - 2
(version: 0)
Comparing performance of:
Create light vs Directly find
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
// 假的 entityById 資料 window.entityById = {}; for (let i = 1; i <= 200; i++) { const price = (i * 10).toString(); // 產生 10, 20, 30, ..., 1000 的 price key const amount = Math.floor(Math.random() * 100) + 1; // 隨機產生 1 到 100 的 amount window.entityById[price] = amount; } // 假的 data 資料 window.data = []; for (let i = 0; i < 20; i++) { const price = ((Math.floor(Math.random() * 100) + 1) * 10).toString(); // 隨機選擇 10, 20, 30, ..., 1000 的 price key const amount = Math.floor(Math.random() * 101); // 隨機產生 0 到 100 的 amount window.data.push([price, amount]); }
Tests:
Create light
function getEntityById(state, data) { const entityById = { ...state.entityById } data.forEach(([price, _amount]) => { const amount = Number(_amount) if (amount === 0) { delete entityById[price] } else { entityById[price] = { amount } } }) return entityById } askEntityById = getEntityById(window.entityById, window.data) const minAsks = Math.min(...Object.keys(askEntityById).map(Number))
Directly find
function getMinAsks(state, data) { const sortedAsks = Object.keys(entityById).sort((a, b) => (Number(a) - Number(b) ? 1: -1)) const idFromData = {} data.forEach(([price, _amount]) => { // array deconstructor is slow const amount = Number(_amount) if (amount === 0) { idFromData[price] = false } else { idFromData[price] = true } }) const sortedIdFromData = Object.keys(idFromData).sort((a, b) => (Number(a) - Number(b) ? 1 : -1)) let minAsk let minStateAsk = sortedAsks.shift() let minDataAsk = sortedIdFromData.shift() while (!minAsk && (sortedAsks.length > 0 || sortedIdFromData.length > 0)) { if (!minDataAsk) { if (idFromData[String(minStateAsk)]) { minAsk = Number(minStateAsk) } else { minStateAsk = sortedAsks.shift() } } else if (!minStateAsk) { if (idFromData[String(minDataAsk)]) { minAsk = Number(minDataAsk) } else { minDataAsk = sortedIdFromData.shift() } } else if (Number(minStateAsk) <= Number(minDataAsk)) { if (idFromData[String(minStateAsk)]) { minAsk = Number(minStateAsk) } else { minStateAsk = sortedAsks.shift() } } else if (Number(minStateAsk) > Number(minDataAsk)) { if (idFromData[String(minDataAsk)]) { minAsk = Number(minDataAsk) } else { minDataAsk = sortedIdFromData.shift() } } } return minAsk } const minAsks = getMinAsks(window.entityById, window.data)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Create light
Directly find
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Create light
255235.0 Ops/sec
Directly find
26099.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its various components to explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined by two test cases: 1. **"Create light"**: This test case uses a function `getEntityById` that takes an entity ID map (`state`) and a data array (`data`). The function filters the data to remove entries with a zero amount, then returns the updated entity ID map. 2. **"Directly find"**: This test case uses another function `getMinAsks` that also takes an entity ID map (`state`) and a data array (`data`). However, it doesn't filter the data; instead, it sorts both the entity ID map and the data array based on their keys/indices. It then iterates through these sorted arrays to find the smallest value. **Options Compared** In both test cases, the following options are compared: * **Sorting**: Both functions sort the data in ascending order by key/index. + Pros: Effective for finding the smallest value; simple to implement. + Cons: May not be suitable for large datasets due to the time complexity of sorting. * **Filtering**: The `getEntityById` function filters out entries with zero amount, while the `getMinAsks` function does not. + Pros: Can reduce the number of iterations needed; faster for small datasets. + Cons: May lead to missed smaller values if the filter is applied early on. **Other Considerations** * **Data Distribution**: The data distribution in both test cases seems uniform, with no extreme outliers. This might favor the sorting approach over filtering, as it would handle all data points more efficiently. * **JavaScript Performance**: The benchmark measures JavaScript performance, so optimizations specific to the language are crucial. Both functions use basic arithmetic operations, but the `getMinAsks` function has an additional loop iteration due to sorting. **Library and Special JS Features** Neither test case explicitly uses any external libraries or special JavaScript features. However, some nuances might be: * **Array Deconstructor**: In the `getMinAsks` function, there's a comment about array deconstruction being slow. While not specific to the benchmark, this suggests that the use of array destructuring in modern JavaScript can sometimes have performance implications. * **Number() Conversion**: The functions use `Number()` to convert string values to numbers. This is a standard JavaScript operation and doesn't impact performance. **Alternatives** Other alternatives for finding the smallest value might include: * **Using a Min-Max Heap Data Structure**: Implementing a heap data structure could provide an efficient way to find the smallest value in O(log n) time. * **Parallel Processing**: Dividing the work among multiple threads or processes could potentially speed up the search process, but would require additional infrastructure and synchronization mechanisms. The `getMinAsks` function's approach relies heavily on sorting both the entity ID map and data array. The `getEntityById` function uses filtering to achieve a similar result with less overhead.
Related benchmarks:
Max and Min values
Max and Min values
Array vs object literal
Array vs object literal v2
lodash groupBy vs Array.reduce 100k with gey generation 2
Comments
Confirm delete:
Do you really want to delete benchmark?