Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object creation: arrow function vs. class
(version: 0)
Functions create a raw object, while class instances can rely on the prototype. Which one is faster?
Comparing performance of:
Create object by function vs Create object by class
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Create object by function
const carFactory = (brand, seats) => ({ brand, seats }) for(let i = 0; i < 20; i++) { carFactory(`brand-${i}`, 5) }
Create object by class
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) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Create object by function
Create object by class
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):
**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: 1. **Arrow Function**: Creating objects using arrow functions, which create a raw object without relying on the prototype chain. 2. **Class**: Creating objects using classes, which can rely on the prototype chain to inherit properties and behavior from parent classes. **Pros and Cons of Each Approach** * **Arrow Functions**: + Pros: Create objects quickly, as they don't require any setup or overhead related to prototypes. + Cons: Don't provide a clear way to define inheritance or polymorphism, as the prototype chain is not accessible. * **Classes**: + Pros: Provide a clear way to define inheritance and polymorphism through the prototype chain. + Cons: Can be slower than arrow functions due to the overhead of creating prototypes. **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: 1. **Create object by function**: This test case uses an arrow function to create objects with two properties: `brand` and `seats`. 2. **Create object by class**: This test case uses a class definition to create objects with the same properties as above. **Latest Benchmark Results** The benchmark results show that: * The arrow function approach is significantly faster than the class approach, with an execution rate of 83041816 executions per second compared to 274366.3125 executions per second for classes. * The browser used for the benchmark is Firefox 112 on a Linux desktop system. **Other Alternatives** If you're interested in exploring other approaches or variations, consider: 1. Using different data structures (e.g., arrays, objects) instead of raw objects. 2. Adding additional properties or methods to the objects being created. 3. Comparing performance with different JavaScript engines (e.g., V8, SpiderMonkey). 4. Investigating the impact of other factors on object creation performance, such as memory allocation or garbage collection.
Related benchmarks:
function vs class method vs new function method v2
ES6 Class vs Prototype vs Object Literal v2
Object creation: arrow function vs. class with methods
Comparison of classes vs prototypes vs object literals also including the inheritance
Comments
Confirm delete:
Do you really want to delete benchmark?