W
webEater
I am trying to implement a class pendant, that takes a data object as
argument and then changes that object instead of extending "this" with
all the data object's data (which means copying much data to a new
object). After some benchmarks I found out, that it's much faster not
to use the conventional new operator.
protoSupport = ({}).__proto__ === Object.prototype;
G = function (data) {
if (protoSupport) data.__proto__ = G.prototype;
else {
for (var k in G.prototype) {
data[k] = G.prototype[k];
}
}
data.z = 1;
return data;
};
G.prototype = {
k: function () {
return this.a;
},
l: function () {
return this.b;
},
constructor: G
};
So we do NOT use the new operator, instead we do:
obj = {a:1, b:2};
G(obj);
INTERESTINGLY, it's even faster, when protoSupport is false and we
iterate manually over all prototype properties. Does somebody know why?
argument and then changes that object instead of extending "this" with
all the data object's data (which means copying much data to a new
object). After some benchmarks I found out, that it's much faster not
to use the conventional new operator.
protoSupport = ({}).__proto__ === Object.prototype;
G = function (data) {
if (protoSupport) data.__proto__ = G.prototype;
else {
for (var k in G.prototype) {
data[k] = G.prototype[k];
}
}
data.z = 1;
return data;
};
G.prototype = {
k: function () {
return this.a;
},
l: function () {
return this.b;
},
constructor: G
};
So we do NOT use the new operator, instead we do:
obj = {a:1, b:2};
G(obj);
INTERESTINGLY, it's even faster, when protoSupport is false and we
iterate manually over all prototype properties. Does somebody know why?