Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
creation
(version: 0)
Comparing performance of:
class vs literal
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
class
class C { x(i) { return i; } } let i = 10000; while (--i) { let c = new C(); c.x(i); }
literal
let i = 10000; while (--i) { const j = i; let c = { x: () => j }; c.x(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
class
literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
class
16559.7 Ops/sec
literal
28206.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's tested in the provided benchmark. **Overview** The provided JSON represents two individual test cases, each with its own script preparation code and HTML preparation code (which is empty in this case). The scripts are designed to create a simple object with an `x` property that returns a value. The main difference between the two test cases lies in how they create the object. **Test Case 1: "class"** In this test case, we have: ```javascript class C { x(i) { return i; } } let i = 10000; while (--i) { let c = new C(); c.x(i); } ``` This script creates a `C` class with an `x` method that takes an integer `i` and returns it. It then creates a new instance of the class in a loop, calling the `x` method on each instance. **Test Case 2: "literal"** In this test case, we have: ```javascript let i = 10000; while (--i) { const j = i; let c = { x: () => j }; c.x(); } ``` This script creates an object `c` with an `x` property that is a function. The function takes no arguments and returns the current value of `j`. It then calls the `x` function. **Options Compared** The two test cases compare the performance of creating objects using classes (`class C`) versus literal objects (`let c = { ... }`). This comparison helps measure which approach is faster for creating objects in JavaScript. **Pros and Cons** * **Classes**: Advantages: + More explicit and readable code + Can provide additional functionality through the constructor (not applicable in this case) Disadvantages: + Creation of a new instance requires memory allocation, which can be slower than literal objects. * **Literal Objects**: Advantages: + Faster creation of simple objects + Less memory overhead Disadvantages: + Code can look less readable and maintainable **Library Used** In neither test case is there an explicit library used. However, it's worth noting that the `x` function in the literal object test case uses a function expression (`() => j`) to create a closure, which might be relevant if we were to extend this benchmark to compare other aspects of JavaScript. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in these test cases. Both scripts use standard JavaScript constructs and do not rely on any advanced features like `let` or `const` declarations with block scope, arrow functions, or template literals. **Alternatives** Other alternatives to measure the performance of object creation could include: * Using a different data structure, such as an array or a Map * Creating objects in a loop using a different approach (e.g., using the `Object.create()` method) * Measuring the performance of different types of objects, such as arrays vs. objects Overall, this benchmark is designed to measure the performance difference between creating objects using classes and literal objects.
Related benchmarks:
createElement vs cloneNode vs cloneNode without validations
javascript new vs Object.create
javascript new vs Object.create 2
javascript new vs Object.create 3
createTextNode vs cloneNode asdf not deep
Comments
Confirm delete:
Do you really want to delete benchmark?