@@ -31,6 +31,23 @@ function encode(value) {
31
31
return JSON . stringify ( toTable ( value ) ) ;
32
32
}
33
33
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
+
34
51
function toTable ( value ) {
35
52
var values = [ ] ;
36
53
var getIndex = makeGetIndexFunction ( values ) ;
@@ -45,15 +62,7 @@ function toTable(value) {
45
62
result = { } ;
46
63
47
64
} 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 ) ;
57
66
58
67
} else {
59
68
for ( var typeName in customTypes ) {
0 commit comments