Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
constructor vs function
(version: 0)
Comparing performance of:
constructor vs function
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class Vector { constructor(x, y) { this.x = x this.y = y } } function createVector(x, y) { return { x, y } } window.Vector = Vector window.createVector = createVector
Tests:
constructor
for (let x = 0; x < 100; x++) { for (let y = 0; y < 100; y++) { new Vector(x, y) } }
function
for (let x = 0; x < 100; x++) { for (let y = 0; y < 100; y++) { createVector(x, y) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
constructor
function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
constructor
75259.1 Ops/sec
function
85236.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is defined in JSON format, which specifies two different approaches for creating a vector object: using a constructor (`class Vector`) versus using a function (`createVector`). The script preparation code defines both the `Vector` class and the `createVector` function, making them available on the global scope. This allows us to compare the performance of these two approaches. **Options Compared** The benchmark compares the performance of two options: 1. **Constructor**: Using the `class Vector` syntax to create a vector object. 2. **Function**: Using the `function createVector(x, y)` syntax to create a vector object. **Pros and Cons** **Constructor Approach:** Pros: * More explicit and self-documenting code * Can be more efficient due to compile-time checks and optimizations Cons: * May require more setup and initialization time * Can lead to overhead from class creation and garbage collection **Function Approach:** Pros: * Faster execution time due to less overhead from class creation and garbage collection * More flexible, as functions can be reused across multiple contexts Cons: * Less explicit and self-documenting code * May require additional setup or initialization time for the function's context **Library: Vector Class** The `Vector` class is not a built-in JavaScript library. Instead, it appears to be a custom implementation created specifically for this benchmark. In this case, the library does not have any significant impact on the performance comparison between the constructor and function approaches. **Special JS Feature or Syntax** There are no special JS features or syntax mentioned in the benchmark code. The tests only utilize standard JavaScript syntax for object creation and function definitions. Now, let's discuss alternative approaches that could be used to compare performance: 1. **Object Literal**: Instead of using a class or function, you could create a vector object directly on the global scope using an object literal (`{ x: 0, y: 0 }`). 2. **Prototype Chain**: You could use the prototype chain to create a new object and assign properties dynamically. 3. **Array**: Create a vector as an array of two elements (e.g., `[x, y]`) and then manipulate it using array methods. Each of these alternatives would require modifications to the benchmark code to accommodate the differences in syntax and execution time.
Related benchmarks:
ES6 Class vs Prototype vs Object Literal v2
Object creation: arrow function vs. class
Instantiation via ES6 Class vs Prototype vs Object Literal
Comparison of classes vs prototypes vs object literals also including the inheritance
ES6 Class vs Prototype vs Object Literal v2 with Extends
Comments
Confirm delete:
Do you really want to delete benchmark?