var A = class{
constructor(){}
setup(){
this.name = "a"
}
}
document.createElement('div')
new A()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
class new |
Test name | Executions per second |
---|---|
createElement | 2683469.0 Ops/sec |
class new | 19551284.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches to create an instance of an object: using the document.createElement()
method and creating a class constructor with the class
keyword (also known as "class syntax").
What is Being Tested?
In this benchmark, we're measuring the performance difference between:
document.createElement('div')
.new
keyword followed by the class constructor.Options Compared
The two options being compared are:
new
keyword.Pros and Cons of Each Approach:
Other Considerations
class
keyword, which is a JavaScript feature introduced in ECMAScript 2015 (ES6). This feature allows for more concise and expressive way of defining classes.Alternatives
If you're looking to create an instance of an object without using the new
keyword, you can also use:
class
, you define a constructor function with the same name as the class.Keep in mind that these alternatives may have different performance characteristics and use cases compared to the new
keyword or the document.createElement()
method.