1
1
'use strict'
2
2
3
3
const benchmark = require ( 'benchmark' )
4
- const safeStringify = require ( 'fast-safe-stringify' )
5
4
const suite = new benchmark . Suite ( )
6
5
7
6
const schema = {
@@ -34,36 +33,25 @@ const obj = {
34
33
age : 32
35
34
}
36
35
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 = [ ]
52
37
53
38
const stringify = require ( '.' ) ( schema )
54
39
const stringifyArray = require ( '.' ) ( arraySchema )
40
+ const stringifyString = require ( '.' ) ( { type : 'string' } )
41
+ var str = ''
55
42
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
+ }
59
49
60
- suite . add ( 'fast-json-stringify' , function ( ) {
61
- stringify ( obj )
62
- } )
50
+ Number ( str )
63
51
64
- suite . add ( 'fast-safe-stringify' , function ( ) {
65
- safeStringify ( obj )
66
- } )
52
+ for ( i = 0 ; i < 1000 ; i ++ ) {
53
+ multiArray . push ( obj )
54
+ }
67
55
68
56
suite . add ( 'JSON.stringify array' , function ( ) {
69
57
JSON . stringify ( multiArray )
@@ -73,18 +61,34 @@ suite.add('fast-json-stringify array', function () {
73
61
stringifyArray ( multiArray )
74
62
} )
75
63
76
- suite . add ( 'fast-safe- stringify array ' , function ( ) {
77
- safeStringify ( multiArray )
64
+ suite . add ( 'JSON. stringify long string ' , function ( ) {
65
+ JSON . stringify ( str )
78
66
} )
79
67
80
- suite . on ( 'complete' , print )
68
+ suite . add ( 'fast-json-stringify long string' , function ( ) {
69
+ stringifyString ( str )
70
+ } )
81
71
82
- suite . run ( )
72
+ suite . add ( 'JSON.stringify short string' , function ( ) {
73
+ JSON . stringify ( 'hello world' )
74
+ } )
83
75
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 ( )
88
91
89
- console . log ( 'Fastest is' , this . filter ( 'fastest' ) . map ( 'name' ) [ 0 ] )
92
+ function cycle ( e ) {
93
+ console . log ( e . target . toString ( ) )
90
94
}
0 commit comments