@@ -6,7 +6,7 @@ See the accompanying LICENSE file for terms.
6
6
7
7
'use strict' ;
8
8
9
- var util = require ( 'util' ) ;
9
+ var isRegExp = require ( 'util' ) . isRegExp ;
10
10
11
11
module . exports = serialize ;
12
12
@@ -24,6 +24,13 @@ var UNICODE_CHARS = {
24
24
'\u2029' : '\\u2029'
25
25
} ;
26
26
27
+ // There's an issue with Node 0.10 (V8 3.14.5.9) which causes `JSON.stringify()`
28
+ // and the subsequent `str.replace()` call to take over 100x more time than when
29
+ // a the `JSON.stringify()` replacer function is used and the data being
30
+ // serialized is very large (500KB). A remedy to this is setting the
31
+ // `JSON.stringify()` `space` option to a truthy value.
32
+ var SPACE = 2 ;
33
+
27
34
function serialize ( obj ) {
28
35
var functions = [ ] ,
29
36
regexps = [ ] ,
@@ -37,12 +44,12 @@ function serialize(obj) {
37
44
return '@__FUNCTION_' + ( functions . push ( value ) - 1 ) + '__@' ;
38
45
}
39
46
40
- if ( util . isRegExp ( value ) ) {
47
+ if ( typeof value === 'object' && isRegExp ( value ) ) {
41
48
return '@__REGEXP_' + ( regexps . push ( value ) - 1 ) + '__@' ;
42
49
}
43
50
44
51
return value ;
45
- } ) ;
52
+ } , SPACE ) ;
46
53
47
54
// Protects against `JSON.stringify()` returning `undefined`, by serializing
48
55
// to the literal string: "undefined".
@@ -57,7 +64,7 @@ function serialize(obj) {
57
64
return UNICODE_CHARS [ unsafeChar ] ;
58
65
} ) ;
59
66
60
- if ( ! ( functions . length || regexps . length ) ) {
67
+ if ( functions . length === 0 && regexps . length === 0 ) {
61
68
return str ;
62
69
}
63
70
0 commit comments