Skip to content

Commit b3901bc

Browse files
committed
Decompose getArrayOfHoles.
1 parent a632931 commit b3901bc

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ function encode(value) {
3131
return JSON.stringify(toTable(value));
3232
}
3333

34+
// This array will grow as needed so that we can slice arrays filled with
35+
// ARRAY_HOLE_INDEX from it.
36+
var HOLY_ARRAY = [];
37+
38+
// Returns an array of the given length filled with ARRAY_HOLE_INDEX.
39+
function getArrayOfHoles(length) {
40+
var holyLen = HOLY_ARRAY.length;
41+
if (length > holyLen) {
42+
HOLY_ARRAY.length = length;
43+
for (var i = holyLen; i < length; ++i) {
44+
HOLY_ARRAY[i] = ARRAY_HOLE_INDEX;
45+
}
46+
}
47+
48+
return HOLY_ARRAY.slice(0, length);
49+
}
50+
3451
function toTable(value) {
3552
var values = [];
3653
var getIndex = makeGetIndexFunction(values);
@@ -45,15 +62,7 @@ function toTable(value) {
4562
result = {};
4663

4764
} else if (Array.isArray(value)) {
48-
result = Array(value.length);
49-
var len = value.length;
50-
if (len > keys.length) {
51-
// The array has holes, so make sure we fill them with the
52-
// ARRAY_HOLE_INDEX constant.
53-
for (var i = 0; i < len; ++i) {
54-
result[i] = ARRAY_HOLE_INDEX;
55-
}
56-
}
65+
result = getArrayOfHoles(value.length);
5766

5867
} else {
5968
for (var typeName in customTypes) {

0 commit comments

Comments
 (0)