Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
class vs proto
(version: 0)
Comparing performance of:
class vs functional w/o static inherit vs functional w/ setproto static inherit
Created:
7 years ago
by:
Registered User
Jump to the latest result
Tests:
class
(()=>{ class Base { static s1() { let n = Math.random() n = n * 1000 let x = 0 while (n) { n = n - 1 x = n } return n } static s2() { return JSON.stringify(window) } p1() { let n = Math.random() << 1 n = n * 1000 >> 1 while (n) { n = n - 1 } return n } p2() { return JSON.stringify(document) } } class Mid extends Base { static ss1() { let n = Math.random() return n << n } static ss2() { return JSON.stringify(window) + 'xxx' } pp1() { const n = Math.random() << 12 return n } pp2() { return JSON.stringify(document) + Date.now() } } class Sub extends Mid {} new Base() new Mid() new Sub() })()
functional w/o static inherit
(()=>{ function Base() {} Base.prototype.constructor = Base Base.s1 = function() { let n = Math.random() n = n * 1000 let x = 0 while (n) { n = n - 1 x = n } return n } Base.s2 = function() { return JSON.stringify(window) } Base.prototype.p1 = function() { let n = Math.random() << 1 n = n * 1000 >> 1 while (n) { n = n - 1 } return n } Base.prototype.p2 = function() { return JSON.stringify(document) } function Mid(){ Base.call(this) } Mid.prototype = Object.create(Mid.prototype) Mid.prototype.constructor = Sub Mid.ss1 = function () { let n = Math.random() return n << n } Mid.ss2= function () { return JSON.stringify(window) + 'xxx' } Mid.prototype.pp1 = function() { const n = Math.random() << 12 return n } Mid.prototype.pp2 = function() { return JSON.stringify(document) + Date.now() } function Sub(){ Mid.call(this) } Sub.prototype = Object.create(Mid.prototype) Sub.prototype.constructor = Sub new Base() new Mid() new Sub() })()
functional w/ setproto static inherit
(()=>{ function Base() {} Base.prototype.constructor = Base Base.s1 = function() { let n = Math.random() n = n * 1000 let x = 0 while (n) { n = n - 1 x = n } return n } Base.s2 = function() { return JSON.stringify(window) } Base.prototype.p1 = function() { let n = Math.random() << 1 n = n * 1000 >> 1 while (n) { n = n - 1 } return n } Base.prototype.p2 = function() { return JSON.stringify(document) } function Mid(){ Base.call(this) } Object.setPrototypeOf(Mid, Base)// !! Mid.prototype = Object.create(Mid.prototype) Mid.prototype.constructor = Sub Mid.ss1 = function () { let n = Math.random() return n << n } Mid.ss2= function () { return JSON.stringify(window) + 'xxx' } Mid.prototype.pp1 = function() { const n = Math.random() << 12 return n } Mid.prototype.pp2 = function() { return JSON.stringify(document) + Date.now() } function Sub(){ Mid.call(this) } Object.setPrototypeOf(Sub, Mid)// !! Sub.prototype = Object.create(Mid.prototype) Sub.prototype.constructor = Sub new Base() new Mid() new Sub() })()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
class
functional w/o static inherit
functional w/ setproto static inherit
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an explanation of the benchmark, comparing options and discussing pros and cons. The provided benchmark tests two approaches to inheritance in JavaScript: 1. **Class-based inheritance** (Test Case: "class") * In this approach, classes are used to define inheritance relationships between `Base`, `Mid`, and `Sub`. This is similar to object-oriented programming (OOP) principles. * The pros of class-based inheritance: + Easier to understand and maintain complex inheritance hierarchies. + Encapsulation and data hiding are naturally enforced by the class system. * However, there are some potential downsides: + JavaScript is a dynamically-typed language, which can lead to type-related issues if not handled properly. + In modern browsers, JavaScript engines often use just-in-time (JIT) compilation, which can impact performance due to slower method lookups. 2. **Functional programming without static inheritance** (Test Case: "functional w/o static inherit") * In this approach, functions are used to define inheritance relationships between `Base` and `Sub`. This is more similar to a functional programming paradigm. * The pros of this approach: + Can be more concise and expressive than class-based inheritance for simple use cases. + Can avoid issues related to object ownership and data hiding. * However, there are some potential downsides: + Can become less readable and maintainable as the number of nested function calls increases. + May not provide the same level of encapsulation as class-based inheritance. **Functional programming with static inheritance** (Test Case: "functional w/ setproto static inherit") * In this approach, functions are used to define inheritance relationships between `Base` and `Sub`, but with the addition of using `Object.setPrototypeOf()` to enforce the prototype chain. * This approach combines elements of both functional and class-based programming paradigms. * The pros of this approach: + Can provide a balance between conciseness and readability, as well as encapsulation. + Can be more efficient than pure functional programming approaches due to the use of `Object.setPrototypeOf()`. * However, there are some potential downsides: + Requires careful consideration of the prototype chain and method lookups to avoid performance issues. The benchmark results show that the execution rates for each approach vary across different browsers and devices. The "functional w/o static inherit" approach tends to perform better on desktop Firefox 116, while the class-based inheritance approach performs better on Windows Firefox 116. The "functional w/ setproto static inherit" approach seems to strike a balance between performance and readability. Keep in mind that these results are specific to this particular benchmark and may not generalize to other scenarios or use cases.
Related benchmarks:
member lookup via prototype
object creation: new, object.create, literal+proto
object creation+method lookup: new, object.create, literal+proto
Comparison of classes vs prototypes vs object literals also including the inheritance
member vs proto member
Comments
Confirm delete:
Do you really want to delete benchmark?