33const benchmark = require ( 'benchmark' )
44const suite = new benchmark . Suite ( )
55
6+ const STR_LEN = 1e4
7+ const LARGE_ARRAY_SIZE = 2e4
8+ const MULTI_ARRAY_LENGHT = 1e3
9+
610const schema = {
711 title : 'Example Schema' ,
812 type : 'object' ,
@@ -89,7 +93,8 @@ const obj = {
8993
9094const date = new Date ( )
9195
92- const multiArray = [ ]
96+ const multiArray = new Array ( MULTI_ARRAY_LENGHT )
97+ const largeArray = new Array ( LARGE_ARRAY_SIZE )
9398
9499const CJS = require ( 'compile-json-stringify' )
95100const CJSStringify = CJS ( schemaCJS )
@@ -99,7 +104,10 @@ const CJSStringifyString = CJS({ type: 'string' })
99104
100105const FJS = require ( '.' )
101106const stringify = FJS ( schema )
102- const stringifyArray = FJS ( arraySchema )
107+ const stringifyArrayDefault = FJS ( arraySchema )
108+ const stringifyArrayJSONStringify = FJS ( arraySchema , {
109+ largeArrayMechanism : 'json-stringify'
110+ } )
103111const stringifyDate = FJS ( dateFormatSchema )
104112const stringifyString = FJS ( { type : 'string' } )
105113let str = ''
@@ -110,18 +118,48 @@ const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
110118const ajvSerializeArray = ajv . compileSerializer ( arraySchemaAJVJTD )
111119const ajvSerializeString = ajv . compileSerializer ( { type : 'string' } )
112120
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+
113137// 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+
115145 str += i
116146 if ( i % 100 === 0 ) {
117147 str += '"'
118148 }
119149}
120150
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+
121159Number ( str )
122160
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
125163}
126164
127165suite . add ( 'FJS creation' , function ( ) {
@@ -138,8 +176,12 @@ suite.add('JSON.stringify array', function () {
138176 JSON . stringify ( multiArray )
139177} )
140178
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 )
143185} )
144186
145187suite . add ( 'compile-json-stringify array' , function ( ) {
@@ -150,6 +192,26 @@ suite.add('AJV Serialize array', function () {
150192 ajvSerializeArray ( multiArray )
151193} )
152194
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+
153215suite . add ( 'JSON.stringify long string' , function ( ) {
154216 JSON . stringify ( str )
155217} )
0 commit comments