class Point1 {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
function Point3(x, y) {
return {
x,
y
}
}
var p1 = new Point1(10, 10);
var p2 = new Point1(10, -10);
var p1 = Point3(10, 10);
var p2 = Point3(10, -10);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
ES6 Class | |
Object Literal |
Test name | Executions per second |
---|---|
ES6 Class | 177128912.0 Ops/sec |
Object Literal | 159914448.0 Ops/sec |
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:
class
keyword.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:
Point1
class, with different values for x
and y
.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
Alternatives
Other alternatives for constructing class objects include:
function Point1(x, y) { ... }
) instead of classes or object literals. While not as modern or efficient as ES6 classes, they are still widely supported.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.