3
3
const benchmark = require ( 'benchmark' )
4
4
const suite = new benchmark . Suite ( )
5
5
6
+ const STR_LEN = 1e4
7
+ const LARGE_ARRAY_SIZE = 2e4
8
+ const MULTI_ARRAY_LENGHT = 1e3
9
+
6
10
const schema = {
7
11
title : 'Example Schema' ,
8
12
type : 'object' ,
@@ -89,7 +93,8 @@ const obj = {
89
93
90
94
const date = new Date ( )
91
95
92
- const multiArray = [ ]
96
+ const multiArray = new Array ( MULTI_ARRAY_LENGHT )
97
+ const largeArray = new Array ( LARGE_ARRAY_SIZE )
93
98
94
99
const CJS = require ( 'compile-json-stringify' )
95
100
const CJSStringify = CJS ( schemaCJS )
@@ -99,7 +104,10 @@ const CJSStringifyString = CJS({ type: 'string' })
99
104
100
105
const FJS = require ( '.' )
101
106
const stringify = FJS ( schema )
102
- const stringifyArray = FJS ( arraySchema )
107
+ const stringifyArrayDefault = FJS ( arraySchema )
108
+ const stringifyArrayJSONStringify = FJS ( arraySchema , {
109
+ largeArrayMechanism : 'json-stringify'
110
+ } )
103
111
const stringifyDate = FJS ( dateFormatSchema )
104
112
const stringifyString = FJS ( { type : 'string' } )
105
113
let str = ''
@@ -110,18 +118,48 @@ const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
110
118
const ajvSerializeArray = ajv . compileSerializer ( arraySchemaAJVJTD )
111
119
const ajvSerializeString = ajv . compileSerializer ( { type : 'string' } )
112
120
121
+ const getRandomString = ( length ) => {
122
+ if ( ! Number . isInteger ( length ) ) {
123
+ throw new Error ( 'Expected integer length' )
124
+ }
125
+
126
+ const validCharacters = 'abcdefghijklmnopqrstuvwxyz'
127
+ const nValidCharacters = 26
128
+
129
+ let result = ''
130
+ for ( let i = 0 ; i < length ; ++ i ) {
131
+ result += validCharacters [ Math . floor ( Math . random ( ) * nValidCharacters ) ]
132
+ }
133
+
134
+ return result [ 0 ] . toUpperCase ( ) + result . slice ( 1 )
135
+ }
136
+
113
137
// eslint-disable-next-line
114
- for ( var i = 0 ; i < 10000 ; i ++ ) {
138
+ for ( let i = 0 ; i < STR_LEN ; i ++ ) {
139
+ largeArray [ i ] = {
140
+ firstName : getRandomString ( 8 ) ,
141
+ lastName : getRandomString ( 6 ) ,
142
+ age : Math . ceil ( Math . random ( ) * 99 )
143
+ }
144
+
115
145
str += i
116
146
if ( i % 100 === 0 ) {
117
147
str += '"'
118
148
}
119
149
}
120
150
151
+ for ( let i = STR_LEN ; i < LARGE_ARRAY_SIZE ; ++ i ) {
152
+ largeArray [ i ] = {
153
+ firstName : getRandomString ( 10 ) ,
154
+ lastName : getRandomString ( 4 ) ,
155
+ age : Math . ceil ( Math . random ( ) * 99 )
156
+ }
157
+ }
158
+
121
159
Number ( str )
122
160
123
- for ( i = 0 ; i < 1000 ; i ++ ) {
124
- multiArray . push ( obj )
161
+ for ( let i = 0 ; i < MULTI_ARRAY_LENGHT ; i ++ ) {
162
+ multiArray [ i ] = obj
125
163
}
126
164
127
165
suite . add ( 'FJS creation' , function ( ) {
@@ -138,8 +176,12 @@ suite.add('JSON.stringify array', function () {
138
176
JSON . stringify ( multiArray )
139
177
} )
140
178
141
- suite . add ( 'fast-json-stringify array' , function ( ) {
142
- stringifyArray ( multiArray )
179
+ suite . add ( 'fast-json-stringify array default' , function ( ) {
180
+ stringifyArrayDefault ( multiArray )
181
+ } )
182
+
183
+ suite . add ( 'fast-json-stringify array json-stringify' , function ( ) {
184
+ stringifyArrayJSONStringify ( multiArray )
143
185
} )
144
186
145
187
suite . add ( 'compile-json-stringify array' , function ( ) {
@@ -150,6 +192,26 @@ suite.add('AJV Serialize array', function () {
150
192
ajvSerializeArray ( multiArray )
151
193
} )
152
194
195
+ suite . add ( 'JSON.stringify large array' , function ( ) {
196
+ JSON . stringify ( largeArray )
197
+ } )
198
+
199
+ suite . add ( 'fast-json-stringify large array default' , function ( ) {
200
+ stringifyArrayDefault ( largeArray )
201
+ } )
202
+
203
+ suite . add ( 'fast-json-stringify large array json-stringify' , function ( ) {
204
+ stringifyArrayJSONStringify ( largeArray )
205
+ } )
206
+
207
+ suite . add ( 'compile-json-stringify large array' , function ( ) {
208
+ CJSStringifyArray ( largeArray )
209
+ } )
210
+
211
+ suite . add ( 'AJV Serialize large array' , function ( ) {
212
+ ajvSerializeArray ( largeArray )
213
+ } )
214
+
153
215
suite . add ( 'JSON.stringify long string' , function ( ) {
154
216
JSON . stringify ( str )
155
217
} )
0 commit comments