11'use strict'
22
33const benchmark = require ( 'benchmark' )
4- const safeStringify = require ( 'fast-safe-stringify' )
54const suite = new benchmark . Suite ( )
65
76const schema = {
@@ -34,36 +33,25 @@ const obj = {
3433 age : 32
3534}
3635
37- const multiArray = [
38- obj ,
39- obj ,
40- obj ,
41- obj ,
42- obj ,
43- obj ,
44- obj ,
45- obj ,
46- obj ,
47- obj ,
48- obj ,
49- obj ,
50- obj
51- ]
36+ const multiArray = [ ]
5237
5338const stringify = require ( '.' ) ( schema )
5439const stringifyArray = require ( '.' ) ( arraySchema )
40+ const stringifyString = require ( '.' ) ( { type : 'string' } )
41+ var str = ''
5542
56- suite . add ( 'JSON.stringify' , function ( ) {
57- JSON . stringify ( obj )
58- } )
43+ for ( var i = 0 ; i < 10000 ; i ++ ) {
44+ str += i
45+ if ( i % 100 === 0 ) {
46+ str += '"'
47+ }
48+ }
5949
60- suite . add ( 'fast-json-stringify' , function ( ) {
61- stringify ( obj )
62- } )
50+ Number ( str )
6351
64- suite . add ( 'fast-safe-stringify' , function ( ) {
65- safeStringify ( obj )
66- } )
52+ for ( i = 0 ; i < 1000 ; i ++ ) {
53+ multiArray . push ( obj )
54+ }
6755
6856suite . add ( 'JSON.stringify array' , function ( ) {
6957 JSON . stringify ( multiArray )
@@ -73,18 +61,34 @@ suite.add('fast-json-stringify array', function () {
7361 stringifyArray ( multiArray )
7462} )
7563
76- suite . add ( 'fast-safe- stringify array ' , function ( ) {
77- safeStringify ( multiArray )
64+ suite . add ( 'JSON. stringify long string ' , function ( ) {
65+ JSON . stringify ( str )
7866} )
7967
80- suite . on ( 'complete' , print )
68+ suite . add ( 'fast-json-stringify long string' , function ( ) {
69+ stringifyString ( str )
70+ } )
8171
82- suite . run ( )
72+ suite . add ( 'JSON.stringify short string' , function ( ) {
73+ JSON . stringify ( 'hello world' )
74+ } )
8375
84- function print ( ) {
85- for ( var i = 0 ; i < this . length ; i ++ ) {
86- console . log ( this [ i ] . toString ( ) )
87- }
76+ suite . add ( 'fast-json-stringify short string' , function ( ) {
77+ stringifyString ( 'hello world' )
78+ } )
79+
80+ suite . add ( 'JSON.stringify obj' , function ( ) {
81+ JSON . stringify ( obj )
82+ } )
83+
84+ suite . add ( 'fast-json-stringify obj' , function ( ) {
85+ stringify ( obj )
86+ } )
87+
88+ suite . on ( 'cycle' , cycle )
89+
90+ suite . run ( )
8891
89- console . log ( 'Fastest is' , this . filter ( 'fastest' ) . map ( 'name' ) [ 0 ] )
92+ function cycle ( e ) {
93+ console . log ( e . target . toString ( ) )
9094}
0 commit comments