function PersonFunction(s) {
this.e = 'hello '+s;
}
PersonFunction.prototype.say = function(){
console.log(this.e)
}
class PersonClass{
constructor(s){
this.e = 'hello '+s;
}
say(){
console.log(this.e)
}
}
var person1 = new PersonFunction('world');
var person2 = new PersonClass('world');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Classes | |
Prototype |
Test name | Executions per second |
---|---|
Classes | 73693336.0 Ops/sec |
Prototype | 76963720.0 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark compares the performance of two approaches: classes and prototypes.
What is being tested?
In this test, a PersonFunction
constructor is defined to create instances of the Person
class. Each instance has an "e" property initialized with a string concatenation expression. A say()
method is also defined on both the prototype and class implementations of the Person
class.
The benchmark tests how fast these two approaches execute, specifically:
PersonFunction
(classes approach)PersonClass
(prototype approach)Options compared
Two options are being compared:
A) Classes: This approach uses a class definition to create instances of the Person
class. The class has its own properties and methods, including the say()
method.
B) Prototype: This approach uses a constructor function (PersonFunction
) that defines a prototype with a say()
method. Instances of this function are created using the new
keyword.
Pros and Cons
Pros:
Cons:
Pros:
Cons:
Library
The PersonClass
is a custom class implementation, likely created for this benchmark. It's not a standard JavaScript library or framework, but rather a simple example used to demonstrate class-based inheritance and encapsulation in JavaScript.
Special JS Feature/Syntax
There are no special features or syntaxes being tested in this benchmark. The code uses standard JavaScript features like classes, prototypes, and constructor functions.
Alternatives
If you're looking for alternative ways to create instances of a class-like object, consider the following:
this
binding to create objects.function Class()
syntax or the Class = function(){...}
method to define class-like constructors.Keep in mind that each of these alternatives has its own trade-offs and may not provide the exact same benefits or drawbacks as classes or prototypes.