Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Generator with low count of items
(version: 0)
Comparing performance of:
ES6 generator vs Closure polyfill
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function* createGenerator() { var i; for (i = 0; i < 0; i++) { yield i; } } function createClosure() { var i = 0; return { next() { var result; if (i < 0) { result = { value: i, done: false }; i++; } else { result = { value: undefined, done: true }; } return result; } }; }
Tests:
ES6 generator
var gen = createGenerator(), result; while (!(result = gen.next()).done) { window.result = result.value; }
Closure polyfill
var gen = createClosure(), result; while (!(result = gen.next()).done) { window.result = result.value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ES6 generator
Closure polyfill
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ES6 generator
14119745.0 Ops/sec
Closure polyfill
7197239.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between two JavaScript approaches: ES6 generators and Closure polyfill (which is essentially a workaround for older browsers that don't support ES6 generators). **Generator with low count of items Benchmark Definition** This definition represents a simple generator function `createGenerator` that yields values from 0 to -1. The script preparation code includes both the ES6 generator and a Closure polyfill implementation. Here's what's being tested: * The `createGenerator` function is executed in two different ways: + Using an ES6 generator (`while (!(result = gen.next()).done)`). + Using a Closure polyfill (`while (!(result = gen.next()).done)`). * The benchmark measures how many times the `next()` method of the generated values are called, which essentially measures the performance of each approach. **Options Compared** The two main options being compared are: 1. **ES6 Generators**: This is a modern JavaScript feature that allows for efficient and concise creation of iterators. It's supported in most modern browsers. 2. **Closure Polyfill**: This is an older approach to creating iterators that was widely used before ES6 generators were introduced. It's still supported by some browsers, but it's generally less efficient than ES6 generators. **Pros and Cons** **ES6 Generators** Pros: * More concise and expressive code * Generally more efficient than Closure polyfill Cons: * Requires support for modern JavaScript features (specifically, iterators) * May not work in older browsers that don't support ES6 generators **Closure Polyfill** Pros: * Works in all browsers, even those that don't support ES6 generators * Can be used to create iterators with a simpler syntax Cons: * Less efficient than ES6 generators * Requires more boilerplate code **Library and Special JS Feature** In this benchmark, the `createGenerator` function uses some special JavaScript features, such as: * **Generators**: The `yield` keyword is used to define an iterator. This feature was introduced in ECMAScript 2015 (ES6) and allows for efficient creation of iterators. * **Iterators**: The `next()` method is used to iterate over the generator values. The Closure polyfill implementation uses a similar approach, but with some differences in syntax and logic. **Other Alternatives** If you're interested in alternative approaches to creating iterators, here are a few options: * **Node.js streams**: Instead of using generators or Closure polyfills, Node.js provides a built-in stream API that allows for efficient creation of iterators. * **Third-party libraries**: There are many third-party libraries available that provide iterator implementations, such as `iterable` or `async-iterator`. * **Manual iteration**: You can also implement manual iteration using loops and conditionals to create an iterator. Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to ES6 generators and Closure polyfill.
Related benchmarks:
Generator
Looping
Making Array Range
Generator and for-of
Comments
Confirm delete:
Do you really want to delete benchmark?