Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 Class vs Prototype vs Object Literal v3
(version: 0)
Test the speed and memory usage using 3 different techniques for constructing class objects.
Comparing performance of:
ES6 Class vs Object Literal
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
class Point1 { constructor(x, y) { this.x = x; this.y = y; } } function Point3(x, y) { return { x, y } }
Tests:
ES6 Class
var p1 = new Point1(10, 10); var p2 = new Point1(10, -10);
Object Literal
var p1 = Point3(10, 10); var p2 = Point3(10, -10);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ES6 Class
Object Literal
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ES6 Class
177128912.0 Ops/sec
Object Literal
159914448.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare three different techniques for constructing class objects in JavaScript: 1. **ES6 Class**: A modern syntax introduced in ECMAScript 2015 (ES6) that allows defining classes using the `class` keyword. 2. **Prototype**: An older syntax that uses prototypes to create new objects, which were the primary way of creating objects before ES6 classes. 3. **Object Literal**: A shorthand way of creating objects using literals, introduced in ECMAScript 5 (ES5). **Script Preparation Code** The provided script preparation code defines two class constructors: * `Point1` uses the ES6 class syntax, with a constructor that takes `x` and `y` parameters. * `Point3` uses the object literal syntax, with a function that returns an object literal with `x` and `y` properties. **Html Preparation Code** The HTML preparation code is empty (`null`), which means this test case doesn't rely on any specific HTML structure or layout. **Individual Test Cases** There are two individual test cases: 1. **ES6 Class**: This test creates two instances of the `Point1` class, with different values for `x` and `y`. 2. **Object Literal**: This test also creates two instances, but using the `Point3` function to create object literals. **Library Usage** None of these tests use any external libraries or frameworks. **Special JS Features/Syntax** The ES6 class syntax uses a feature introduced in ECMAScript 2015 (ES6). This feature is widely supported by modern browsers and JavaScript engines, including Chrome and other popular browsers. **Test Results** According to the latest benchmark results, the Object Literal approach outperforms both ES6 Class and Prototype approaches. The raw UString from the test result indicates that the browser used is Chrome Mobile 122 on an Android device, with a high number of executions per second (7231853.0). **Pros and Cons** * **ES6 Class**: + Pros: Modern syntax, more expressive, and efficient in some cases. + Cons: Not supported by older browsers or environments, may have compatibility issues. * **Prototype**: + Pros: Widely supported across browsers and environments, simple to implement. + Cons: Less expressive than ES6 classes, may lead to longer execution times due to the overhead of prototype chains. * **Object Literal**: + Pros: Fastest execution time in this benchmark, widely supported by modern browsers and JavaScript engines. + Cons: Less expressive than other approaches, requires more code to create complex objects. **Alternatives** Other alternatives for constructing class objects include: * **Function Constructors**: Using function constructors (e.g., `function Point1(x, y) { ... }`) instead of classes or object literals. While not as modern or efficient as ES6 classes, they are still widely supported. * **Module-Based Classes**: Using classes defined in modules (e.g., `import { Point1 } from './Point1.js';`) to create modular and reusable code. These alternatives may have varying performance characteristics and trade-offs compared to the tested approaches.
Related benchmarks:
Variable assignment from object | traditional vs destructuring
Object vs toString
javascript new vs Object.create 2
javascript new vs Object.create 3
new object vs new instance of new class
Comments
Confirm delete:
Do you really want to delete benchmark?