Skip to content

Commit eabcd99

Browse files
committed
Inline ownEnumerableKeys into for-loop
This gives 3x performance boost in Node 0.12 and later (and 5x on Node 0.10.x)
1 parent 02622dc commit eabcd99

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

bench.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var assert = require('assert');
2+
var lodash = require('lodash');
3+
var objectAssign = require('./');
4+
5+
var source1 = {a: 1, b: 2, c: 3};
6+
var source2 = {c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 15, o: 15, p: 16};
7+
8+
suite('object-assign', function () {
9+
bench('small', function () {
10+
objectAssign({foo: 0}, {bar: 1});
11+
});
12+
13+
bench('default options', function () {
14+
objectAssign({}, {foo: 0}, {foo: 1});
15+
});
16+
17+
bench('big', function () {
18+
objectAssign({}, source1, source2);
19+
});
20+
});
21+
22+
suite('lodash', function () {
23+
bench('small', function () {
24+
lodash.assign({foo: 0}, {bar: 1});
25+
});
26+
27+
bench('default options', function () {
28+
lodash.assign({}, {foo: 0}, {foo: 1});
29+
});
30+
31+
bench('big', function () {
32+
lodash.assign({}, source1, source2);
33+
});
34+
});

index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
var propIsEnumerable = Object.prototype.propertyIsEnumerable;
2+
var hasOwnProperty = Object.prototype.hasOwnProperty;
33

44
function ToObject(val) {
55
if (val == null) {
@@ -9,29 +9,26 @@ function ToObject(val) {
99
return Object(val);
1010
}
1111

12-
function ownEnumerableKeys(obj) {
13-
var keys = Object.getOwnPropertyNames(obj);
14-
15-
if (Object.getOwnPropertySymbols) {
16-
keys = keys.concat(Object.getOwnPropertySymbols(obj));
17-
}
18-
19-
return keys.filter(function (key) {
20-
return propIsEnumerable.call(obj, key);
21-
});
22-
}
23-
2412
module.exports = Object.assign || function (target, source) {
2513
var from;
2614
var keys;
2715
var to = ToObject(target);
16+
var symbols;
2817

2918
for (var s = 1; s < arguments.length; s++) {
30-
from = arguments[s];
31-
keys = ownEnumerableKeys(Object(from));
19+
from = Object(arguments[s]);
20+
21+
for (var key in from) {
22+
if (hasOwnProperty.call(from, key)) {
23+
to[key] = from[key];
24+
}
25+
}
3226

33-
for (var i = 0; i < keys.length; i++) {
34-
to[keys[i]] = from[keys[i]];
27+
if (Object.getOwnPropertySymbols) {
28+
symbols = Object.getOwnPropertySymbols(from);
29+
for (var i = 0; i < symbols.length; i++) {
30+
to[symbols[i]] = from[symbols[i]];
31+
}
3532
}
3633
}
3734

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"node": ">=0.10.0"
1414
},
1515
"scripts": {
16-
"test": "mocha"
16+
"test": "mocha",
17+
"bench": "matcha bench.js"
1718
},
1819
"files": [
1920
"index.js"
@@ -33,6 +34,8 @@
3334
"browser"
3435
],
3536
"devDependencies": {
37+
"lodash": "^3.10.1",
38+
"matcha": "^0.6.0",
3639
"mocha": "*"
3740
}
3841
}

0 commit comments

Comments
 (0)