const carFactory = (brand, seats) => ({
brand, seats
})
for(let i = 0; i < 20; i++) {
carFactory(`brand-${i}`, 5)
}
class Car {
brand;
seats;
constructor(brand, seats) {
this.brand = brand;
this.seats = seats;
}
}
for(let i = 0; i < 20; i++) {
new Car(`brand-${i}`, 5)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Create object by function | |
Create object by class |
Test name | Executions per second |
---|---|
Create object by function | 83041816.0 Ops/sec |
Create object by class | 274366.3 Ops/sec |
Overview of the Benchmark
The provided benchmark compares the performance of creating objects using arrow functions versus classes in JavaScript.
Comparison Options
Two options are compared:
Pros and Cons of Each Approach
Library Usage
None of the test cases use any external libraries. The benchmark only relies on built-in JavaScript features.
Special JS Features/Syntax
No special JavaScript features or syntax are used in this benchmark.
Benchmark Preparation Code and JSON
The benchmark preparation code is empty, which means that the script itself creates the objects being measured.
Individual Test Cases
Each test case measures the time it takes to create 20 objects using a specific approach:
brand
and seats
.Latest Benchmark Results
The benchmark results show that:
Other Alternatives
If you're interested in exploring other approaches or variations, consider: