Created
April 24, 2020 19:56
-
-
Save DerekZiemba/4ef29e47641e4fa1fdb8af48b2a7fe12 to your computer and use it in GitHub Desktop.
Javascript Property Access Performance (http://jsbench.github.io/#4ef29e47641e4fa1fdb8af48b2a7fe12) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Javascript Property Access Performance</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
function testObject(obj) { | |
obj.bool = !obj.bool; | |
obj.tmp = obj.float - 0.69 + obj.self.self.float; | |
obj.integer = obj.integer + obj.string.length + obj.array.length; | |
obj.self.object.some = obj.tmp + obj.object.some; | |
if (!obj.bool) { | |
obj.tmp = obj.string; | |
obj.string = obj.stringB; | |
obj.stringB = obj.integer.toString(); | |
obj.self.self.self.array.push(obj.tmp); | |
} else { | |
obj.tmp = obj.array.pop(); | |
obj.stringB = obj.self.self.string; | |
obj.string = obj.tmp; | |
} | |
obj.string = obj.self.self.string; | |
obj.self.self.array = obj.array; | |
return obj.self.object; | |
} | |
function DirectValue() { | |
this.tmp = null; | |
this.bool = true; | |
this.integer = 42; | |
this.float = 420.69; | |
this.string = 'test string'; | |
this.stringB = 'some other string'; | |
this.array = ['array', 'of', 'strings']; | |
this.object = { some: 1 }; | |
this.self = this; | |
} | |
function DirectNonEnumerable() { | |
Object.defineProperty(this, 'tmp', { value: null, configurable: true, writable: true }); | |
Object.defineProperty(this, 'bool', { value: true, configurable: true, writable: true }); | |
Object.defineProperty(this, 'integer', { value: 42, configurable: true, writable: true }); | |
Object.defineProperty(this, 'float', { value: 420.69, configurable: true, writable: true }); | |
Object.defineProperty(this, 'string', { value: 'test string', configurable: true, writable: true }); | |
Object.defineProperty(this, 'stringB', { value: 'some other string', configurable: true, writable: true }); | |
Object.defineProperty(this, 'array', { value: ['array', 'of', 'strings'], configurable: true, writable: true }); | |
Object.defineProperty(this, 'object', { value: { some: 1 }, configurable: true, writable: true }); | |
Object.defineProperty(this, 'self', { value: this, configurable: true, writable: true }); | |
} | |
function ProtoValue() { this.self = this; } | |
ProtoValue.prototype = { | |
tmp: null, | |
bool: true, | |
integer: 42, | |
float: 420.69, | |
string: 'test string', | |
stringB: 'some other string', | |
array: ['array', 'of', 'strings'], | |
object: { some: 1 } | |
}; | |
function ProtoNonEnumerable() { | |
Object.defineProperty(this, 'self', { value: this, configurable: true, writable: true }); | |
} | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'tmp', { value: null, configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'bool', { value: true, configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'integer', { value: 42, configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'float', { value: 420.69, configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'string', { value: 'test string', configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'stringB', { value: 'some other string', configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'array', { value: ['array', 'of', 'strings'], configurable: true, writable: true }); | |
Object.defineProperty(ProtoNonEnumerable.prototype, 'object', { value: { some: 1 }, configurable: true, writable: true }); | |
var directValue = new DirectValue(); | |
var directNonEnum = new DirectNonEnumerable(); | |
var protoValue = new ProtoValue(); | |
var protoNonEnum = new ProtoNonEnumerable(); | |
}; | |
suite.add("directValue", function () { | |
// directValue | |
testObject(directValue); | |
}); | |
suite.add("directNonEnum", function () { | |
// directNonEnum | |
testObject(directNonEnum); | |
}); | |
suite.add("protoValue", function () { | |
// protoValue | |
testObject(protoValue); | |
}); | |
suite.add("protoNonEnum", function () { | |
// protoNonEnum | |
testObject(protoNonEnum); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Javascript Property Access Performance"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment