Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js custom yield vs base ma
(version: 0)
Comparing performance of:
yield vs base
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
yield
const arr = []; for (let i = 0; i < 100000; i++) { const obj = {}; for (let j = 0; j < 10; j++) { obj[`prop_${j}`] = j; } arr.push(obj); } const where = function*(isMatch) { for (let item of this) { if (isMatch(item)) { yield item; } } }; const select = function*(fn) { for (let item of this) { yield fn(item); } }; const toArray = function() { return Array.from(this); }; const Generator = Object.getPrototypeOf(function*() {}); Generator.prototype.where = where; Array.prototype.where = where; Generator.prototype.select = select; Array.prototype.select = select; Generator.prototype.toArray = toArray; Array.prototype.toArray = toArray; arr.select((obj) => obj.prop_3) .where((num) => num > 5) .toArray();
base
const arr = []; for (let i = 0; i < 100000; i++) { const obj = {}; for (let j = 0; j < 10; j++) { obj[`prop_${j}`] = j; } arr.push(obj); } const where = function*(isMatch) { for (let item of this) { if (isMatch(item)) { yield item; } } }; const select = function*(fn) { for (let item of this) { yield fn(item); } }; const toArray = function() { return Array.from(this); }; const Generator = Object.getPrototypeOf(function*() {}); Generator.prototype.where = where; Array.prototype.where = where; Generator.prototype.select = select; Array.prototype.select = select; Generator.prototype.toArray = toArray; Array.prototype.toArray = toArray; arr.map((obj) => obj.prop_3).filter((num) => num > 5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
yield
base
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
yield
16.8 Ops/sec
base
17.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to filter data from an array: `yield` (custom implementation) and `base` (using standard JavaScript methods). The test case creates a large array of objects with properties, and then uses two different filtering functions to extract specific values. **What is Being Tested?** The benchmark measures the performance difference between using a custom iterator (`yield`) approach versus using standard JavaScript methods (`map` and `filter`). Specifically, it tests: 1. Filtering an array of objects based on a property value using a custom iterator. 2. Filtering an array of objects based on a property value using standard `map` and `filter` methods. **Custom Yield Approach** The custom yield approach uses two functions: `where` and `select`. These functions are used to filter the data in both tests: * `where`: takes a callback function as an argument, which is applied to each item in the array. If the callback returns true, the item is yielded. * `select`: similar to `where`, but applies a transformation function to each item before yielding. The custom iterator (`yield`) approach uses these functions to filter the data. In the test case, it's used with two different filtering criteria: extracting objects with specific property values and then further filtering based on another value. **Standard Base Approach** The standard base approach simply uses `map` and `filter` methods to achieve the same result: * `map`: applies a transformation function to each item in the array. * `filter`: takes a callback function as an argument, which is applied to each item in the array. If the callback returns true, the item is included. The standard base approach is likely implemented using built-in JavaScript methods, whereas the custom yield approach uses custom iterator functions. **Pros and Cons** **Custom Yield Approach:** Pros: * Allows for fine-grained control over filtering logic. * Can be optimized for specific use cases. Cons: * More complex to implement and maintain compared to standard methods. * May have performance overhead due to custom iteration logic. **Standard Base Approach:** Pros: * Faster execution speed, as it leverages built-in JavaScript methods. * Easier to understand and maintain, especially for developers familiar with standard libraries. Cons: * Less control over filtering logic, which might limit optimization opportunities. **Other Considerations** When comparing the custom yield approach to the standard base approach, consider factors such as: * Performance: How do the two approaches compare in terms of execution speed? * Code readability and maintainability: Which approach is easier to understand and modify for different use cases? * Flexibility: Can the custom yield approach be adapted to new filtering criteria or data structures? **Alternatives** Other alternatives for achieving similar results could include: * Using libraries like Lodash, which provides a rich set of utility functions for data manipulation. * Implementing a custom sorting function using `Array.prototype.sort` and then applying filtering logic. * Utilizing modern JavaScript features like `reduce()` or `forEach()`, which can provide more concise filtering solutions. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
math pow vs multiply vs reduce
Math.pow vs Exponentiation vs Multiplication
math pow vs multiply vs double asterix
Math.pow vs Exponentiation vs Multiplication pow 4
Math.pow vs ** vs * (power of 2)
Comments
Confirm delete:
Do you really want to delete benchmark?