Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test a - b
(version: 0)
Comparing performance of:
a vs b
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// filterFoo = (x) => x.action === "BID" && x.symbol.toLocaleLowerCase().includes('btc') sortFoo = (a, b) => { return a.price - b.price } reduceFoo = (value, item) => { return item.action === "BID" && item.symbol.toLocaleLowerCase().includes('btc') && item?.price >= value ? item?.price : value; } // arr = [{ price: 1000000000, action: "ASK", symbol: "BTC_IRT" }, { price: 1000000, action: "ASK", symbol: "BTC_IRT" }, { price: 111111, action: "BID", symbol: "BTC_IRT" }, { price: 100000, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "BID", symbol: "BTC_IRT" }, { price: 10000, action: "BID", symbol: "BTC_IRT" }, { price: 1111, action: "BID", symbol: "BTC_IRT" }, { price: 1000, action: "BID", symbol: "BTC_IRT" }, { price: 100, action: "BID", symbol: "BTC_IRT" }, { price: 1, action: "BID", symbol: "BTC_IRT" }, { price: 11111, action: "ASK", symbol: "BTC_USDT" }, { price: 25000, action: "ASK", symbol: "USDT_IRT" }, { price: 11111, action: "ASK", symbol: "USDT_IRT" }, { price: 10000, action: "ASK", symbol: "USDT_IRT" }, { price: 1111, action: "BID", symbol: "USDT_IRT" }]
Tests:
a
const sorted = arr.filter(filterFoo).sort(sortFoo) const bigPrice = sorted[0].price
b
const bigPrice = arr.reduce(reduceFoo, 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
a
b
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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition JSON** The benchmark definition represents a script that defines three functions: `filterFoo`, `sortFoo`, and `reduceFoo`. These functions are used to filter, sort, and reduce an array (`arr`) containing objects with `price`, `action`, and `symbol` properties. * `filterFoo`: takes an object `x` as input and returns `true` if the action is "BID" and the symbol contains "btc" (case-insensitive). This function filters the array to only include objects that match these conditions. * `sortFoo`: compares two objects `a` and `b` based on their `price` property, returning `-1` if `a.price` is less than `b.price`, `0` if they're equal, and `1` if `a.price` is greater than `b.price`. This function sorts the filtered array in ascending order of price. * `reduceFoo`: takes an initial value `value` (set to 0) and iterates over the array. For each object `item`, it checks if the action is "BID", the symbol contains "btc", and the item's price is greater than or equal to the current value. If any of these conditions are true, it updates the value with the item's price. The script then creates an array (`arr`) containing objects with various `price`, `action`, and `symbol` values. The filtered and sorted array is stored in the `sorted` variable, and its first element's `price` property is assigned to the `bigPrice` constant. **Individual Test Cases** There are two test cases: 1. `Test Name: "a"`: This test case uses the `filterFoo`, `sortFoo`, and array operations to filter, sort, and reduce the original array. The benchmark measures the execution time of this process. 2. `Test Name: "b"`: Similar to Test Case "a", but instead of using `filterFoo` and `sortFoo`, it uses the `reduceFoo` function directly on the original array. **Benchmark Results** The latest benchmark results show two browsers (Chrome 91) running different versions of Windows, both on Desktop devices. The results are as follows: * Test Name: "b" - Execution rate: approximately 267,624 executions per second * Test Name: "a" - Execution rate: approximately 326,003 executions per second These numbers indicate that the `reduceFoo` approach is slightly faster than the `filterFoo` and `sortFoo` approach in this specific benchmark. **Library Used** In both test cases, a library called Lodash (not explicitly mentioned in the provided JSON) is implicitly used. Specifically: * The `filter`, `map`, `sort`, and `reduce` functions are part of the Lodash library. * The `toLocaleLowerCase()` method is also a built-in JavaScript function that can be used with or without Lodash. **Special JS Features/Syntax** This benchmark doesn't explicitly use any special JavaScript features or syntax beyond what's considered standard (e.g., arrow functions, template literals, etc.). **Alternatives** If you were to rewrite this benchmark using different approaches, here are some alternatives: * **No library**: Remove the Lodash library and implement `filter`, `sort`, and `reduce` manually. * **Native JavaScript arrays only**: Remove any non-standard libraries or functions and rely solely on native JavaScript array methods. * **Other sorting algorithms**: Instead of using a simple sort, explore alternative sorting algorithms like merge sort or heap sort. When rewriting the benchmark, keep in mind that optimizations may vary depending on the specific requirements and target environments.
Related benchmarks:
filter.map vs reduce 2
sort-filter vs reduce
Simple filter sort vs reduce
Filter + pop vs Filter + at
Comments
Confirm delete:
Do you really want to delete benchmark?