Klass = function(p) {
this.p = p;
}
Klass.prototype.foo = function(p) {
console.log('foo ' + p + ' ' + this.p);
}
var i = 100000,
klass = new Klass('wrapped');
while(i--) {
wrapper(i);
}
function wrapper(p) {
klass.foo(p);
}
var i = 100000,
klass = new Klass('wrapped');
while(i--) {
klass.foo(i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Wrapped Call | |
Direct Call |
Test name | Executions per second |
---|---|
Wrapped Call | 0.4 Ops/sec |
Direct Call | 0.4 Ops/sec |
Let's dive into the explanation of what's being tested in this benchmark.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that tests the performance difference between calling a method on an object using two different approaches:
Klass
) and passing it to a wrapper function, which then calls the foo
method on the object.foo
method directly on the klass
object, without any wrapping or additional functions.Options Compared
The benchmark is comparing the performance of these two approaches:
foo
method directly on the klass
object.Pros and Cons
Here are some pros and cons of each approach:
foo
method requires specific initialization or setup.Library and Its Purpose
In this benchmark, Klass
is a custom class definition using JavaScript's prototype-based object creation. The foo
method on the Klass.prototype
object is intended to demonstrate how method calls are executed on objects in different scenarios.
Special JS Feature or Syntax
There isn't any specific special feature or syntax being tested in this benchmark, other than the use of prototype-based object creation and methods.
Other Alternatives
If you're interested in exploring alternative approaches for similar benchmarks, here are some alternatives:
Klass.foo(p)
).bind()
or an arrow function).Keep in mind that these alternatives might change the focus of the benchmark and may not directly compare to the wrapped call and direct call approaches tested here.