window.AClass = class {
b() {
return 4;
}
}
window.AFunc = function() {
return {
b() {
return 4;
}
};
};
const a = new AClass();
a.b();
const a = new AFunc();
a.b();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Class instance method lookup | |
Function-created object method lookup |
Test name | Executions per second |
---|---|
Class instance method lookup | 68332160.0 Ops/sec |
Function-created object method lookup | 31726560.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare two approaches: class instance method lookup and function-created object method lookup.
Class Instance Method Lookup
In this approach, an instance of the AClass
class is created and its b()
method is invoked directly on the instance. The b()
method returns a value, which is then executed and measured by the benchmark.
This approach tests how JavaScript engines execute methods on objects that are instances of classes. It's essential to note that this type of lookup is commonly used in object-oriented programming (OOP) scenarios.
Function-Created Object Method Lookup
In this approach, an object is created using a function expression (window.AFunc = function() {...}
), and its b()
method is invoked directly on the object. The b()
method also returns a value, which is then executed and measured by the benchmark.
This approach tests how JavaScript engines execute methods on objects that are not instances of classes but rather objects created using function expressions.
Pros and Cons
Both approaches have their pros and cons:
Library
There doesn't appear to be any external library used in this benchmark.
Special JS Features or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on understanding how different approaches affect performance.
Other Alternatives
To create a similar benchmark, you could consider the following alternatives:
this
keyword to test method invocation on objects.new AFunc()
).Keep in mind that these alternatives might require adjustments to the benchmark's setup and script preparation code.