function s(obj){}
function C1() {
this.c = 0;
}
C1.prototype.test = function () {
this.c++;
}
function C2() {
this.c = 0;
this.test = function () {
this.c++;
}
}
var c1 = new C1();
var c2 = new C2();
c1.test();
c2.test();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
c1.test(); | |
c2.test(); |
Test name | Executions per second |
---|---|
c1.test(); | 11010402.0 Ops/sec |
c2.test(); | 11176119.0 Ops/sec |
Let's break down the provided benchmark and its components.
Benchmark Definition
The benchmark is defined using a JSON object, which includes:
Name
: The name of the benchmark, in this case, "prototype function".Description
: An empty string, indicating that no detailed description is available for this benchmark.Script Preparation Code
:s(obj)
and the definition of classes C1
and C2
. These classes have a commonality in that they create an instance with a property c
and a method test
, which increments the value of c
.Html Preparation Code
: An empty string, indicating that no HTML code is necessary for this benchmark.Individual Test Cases
The benchmark consists of two test cases:
c1.test();
c2.test();
These test cases are defined by providing a specific JavaScript statement to execute within the context of each class (C1
and C2
, respectively).
Comparison of Options
In this case, the options being compared are the performance differences between executing a standalone method versus creating an instance of a class with a specific method:
c1.test();
, we execute the test()
method directly on the global object (i.e., this
is not explicitly defined). This option might be faster, as it eliminates the overhead of creating an instance and binding the method to that instance.c2.test();
, we create an instance of class C1
or C2
and execute the test()
method on that instance. This option includes the overhead of object creation, binding the method to the instance, and accessing the instance's properties.Pros and Cons
Library Usage
There is no explicit library usage mentioned in this benchmark. However, it's worth noting that some JavaScript engines might have their own internal libraries or optimizations that could affect the execution time of these benchmarks.
Special JS Features
There are a few special JavaScript features used here:
C1
and C2
classes demonstrate how to define constructors and prototype methods in JavaScript.this
is implicitly bound to the global object or the instance of the class. This syntax highlights how this
works when defining functions within a constructor.Alternatives
If this benchmark were to be rewritten for different programming languages or environments, here are some alternatives that might be explored: