var Klass = function() {}
Klass.prototype.foo = function(p) {
console.log('foo ' + p);
}
function free(p) {
console.log('foo ' + p);
}
var i = 100000,
klass = new Klass();
while(i--) {
klass.foo(i);
}
var i = 100000;
while(i--) {
free(i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object Function | |
Free Function |
Test name | Executions per second |
---|---|
Object Function | 1.0 Ops/sec |
Free Function | 0.9 Ops/sec |
Let's dive into the benchmark and explore what's being tested.
Benchmark Definition
The benchmark is defined by two JavaScript functions: Klass
(an object function) and free
(a free function, also known as a standalone function). Both functions have a single method foo
, which logs a message to the console.
Options Compared
Two options are compared:
Klass
class is used to create an instance of the object function. The foo
method is called on this instance, passing a parameter p
.free
standalone function is called directly, without creating any context or instance. It also takes a single parameter p
.Pros and Cons
Object Function (Klass)
Pros:
Cons:
Free Function (free)
Pros:
Cons:
Library/Utility
In this benchmark, the Klass
class is used as a utility function to create an object instance. It's likely that Klass
is designed to be a lightweight, minimalistic class factory, rather than a full-fledged class library.
Special JS Feature/Syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond the two functions being compared. However, it's worth noting that using classes and functions in this way can take advantage of modern JavaScript features like closures and prototypes, which are still widely supported across browsers.
Alternatives
Other alternatives to compare these options might include:
Klass
as a closure, creating an instance on the fly.foo
without creating an instance.Keep in mind that these alternatives would likely change the nature of the benchmark and the trade-offs involved.