Skip to content

Instantly share code, notes, and snippets.

@mranney
Last active August 29, 2015 14:15
Show Gist options
  • Save mranney/d383ed6488985ee6185a to your computer and use it in GitHub Desktop.
Save mranney/d383ed6488985ee6185a to your computer and use it in GitHub Desktop.
function SomeClass() {
this.version = null;
this.header_length = null;
this.header_bytes = null;
this.diffserv = null;
this.total_length = null;
this.identification = null;
this.flags = {};
this.fragment_offset = null;
this.ttl = null;
this.protocol = null;
this.header_checksum = null;
this.saddr = null;
this.daddr = null;
this.protocol_name = null;
this.payload = null;
}
SomeClass.prototype.decode = function () {
this.version = 4;
this.header_length = 1;
this.header_bytes = 2
this.diffserv = 3;
this.total_length = 4;
this.identification = 5;
this.flags.reserved = 6;
this.flags.df = 7;
this.flags.mf = 8;
this.fragment_offset = 9;
this.ttl = 10;
this.protocol = 11;
this.header_checksum = 12;
this.saddr = 13;
this.daddr = 14;
};
function SomeClass2() {
}
SomeClass2.prototype.decode = function () {
this.version = 4;
this.header_length = 1;
this.header_bytes = 2
this.diffserv = 3;
this.total_length = 4;
this.identification = 5;
this.flags = {};
this.flags.reserved = 6;
this.flags.df = 7;
this.flags.mf = 8;
this.fragment_offset = 9;
this.ttl = 10;
this.protocol = 11;
this.header_checksum = 12;
this.saddr = 13;
this.daddr = 14;
};
var start = Date.now();
for (var i = 0; i < 10000000; i++) {
var a = new SomeClass().decode();
}
var dur = Date.now() - start;
console.log("init constructor in " + dur + "ms");
var start = Date.now();
for (var i = 0; i < 10000000; i++) {
var a = new SomeClass2().decode();
}
var dur = Date.now() - start;
console.log("empty constructor in " + dur + "ms");
var start = Date.now();
for (var i = 0; i < 10000000; i++) {
var a = new SomeClass().decode();
}
var dur = Date.now() - start;
console.log("init constructor in " + dur + "ms");
var start = Date.now();
for (var i = 0; i < 10000000; i++) {
var a = new SomeClass2().decode();
}
var dur = Date.now() - start;
console.log("empty constructor in " + dur + "ms");
$ node ./new_test.js
init constructor in 884ms
empty constructor in 1103ms
init constructor in 1073ms
empty constructor in 1263ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment