Skip to content

Commit 727bb23

Browse files
committed
Improve performance of toJS (#1453)
1 parent e65e5af commit 727bb23

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/toJS.js

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

88
import { Seq } from './Seq';
9+
import { isIndexed } 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 (isDataStructure(value)) {
14+
value = Seq(value);
15+
const result = isIndexed(value) ? [] : {};
16+
value.forEach((v, k) => {
17+
result[k] = toJS(v);
18+
});
19+
return result;
20+
}
21+
return value;
1722
}

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)