Skip to content

Commit 43835aa

Browse files
committed
Improve performance of toJS (#1581)
Fixes #1453 commit b600bd7c56da9f71d19a035838523610d4c96c00 Author: Lee Byron <lee.byron@robinhood.com> Date: Tue Sep 18 12:54:40 2018 -0700 Further improvements commit 9c6395b4baba8a79b3b364d68a4909a26ce7b51c Merge: ca85e75 26ecbab Author: Lee Byron <lee.byron@robinhood.com> Date: Tue Sep 18 12:28:49 2018 -0700 Merge branch 'toJS-performance' of https://github.com/lukaswelinder/immutable-js into lukaswelinder-toJS-performance commit 26ecbab Author: Lukas Welinder <lukas@welinder.co> Date: Fri Aug 24 20:28:00 2018 -0700 Resolve issue w/ toJS handling structures that should become arrays commit 1c56169 Author: Lukas Welinder <lukas@welinder.co> Date: Fri Aug 24 20:11:42 2018 -0700 Resolve issue w/ toJS handling structures that should become arrays commit 0b44fad Author: Lukas Welinder <lukas@welinder.co> Date: Fri Aug 24 20:01:39 2018 -0700 Revert "Adjust type check approach for toJS performance fix" This reverts commit 0ca7f5d commit 0ca7f5d Author: Lukas Welinder <lukas@welinder.co> Date: Fri Aug 24 19:57:19 2018 -0700 Adjust type check approach for toJS performance fix commit 727bb23 Author: Lukas Welinder <lukas@welinder.co> Date: Fri Aug 24 19:38:01 2018 -0700 Improve performance of toJS (#1453)
1 parent ca85e75 commit 43835aa

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

perf/toJS.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
describe('toJS', () => {
9+
const array32 = [];
10+
for (let ii = 0; ii < 32; ii++) {
11+
array32[ii] = ii;
12+
}
13+
const list = Immutable.List(array32);
14+
15+
it('List of 32', () => {
16+
Immutable.toJS(list);
17+
});
18+
19+
const obj32 = {};
20+
for (let ii = 0; ii < 32; ii++) {
21+
obj32[ii] = ii;
22+
}
23+
const map = Immutable.Map(obj32);
24+
25+
it('Map of 32', () => {
26+
Immutable.toJS(map);
27+
});
28+
});

src/toJS.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@
66
*/
77

88
import { Seq } from './Seq';
9+
import { isCollection, isKeyed } from './Predicates';
910
import isDataStructure from './utils/isDataStructure';
1011

1112
export function toJS(value) {
12-
return isDataStructure(value)
13-
? Seq(value)
14-
.map(toJS)
15-
.toJSON()
16-
: value;
13+
if (!value || typeof value !== 'object') {
14+
return value;
15+
}
16+
if (!isCollection(value)) {
17+
if (!isDataStructure(value)) {
18+
return value;
19+
}
20+
value = Seq(value);
21+
}
22+
if (isKeyed(value)) {
23+
const result = {};
24+
value.__iterate((v, k) => {
25+
result[k] = toJS(v);
26+
});
27+
return result;
28+
}
29+
const result = [];
30+
value.__iterate(v => {
31+
result.push(toJS(v));
32+
});
33+
return result;
1734
}

src/utils/isDataStructure.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ import isPlainObj from './isPlainObj';
1313
* provided by Immutable.js or a plain Array or Object.
1414
*/
1515
export default function isDataStructure(value) {
16-
return isImmutable(value) || Array.isArray(value) || isPlainObj(value);
16+
return (
17+
typeof value === 'object' &&
18+
(isImmutable(value) || Array.isArray(value) || isPlainObj(value))
19+
);
1720
}

0 commit comments

Comments
 (0)