fast-json-stringify 2.7.9 → 2.7.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +13 -10
  2. package/bench.js +28 -0
  3. package/index.js +8 -14
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -12,22 +12,25 @@ __fast-json-stringify__ is significantly faster than `JSON.stringify()` for smal
12
12
  ##### Benchmarks
13
13
 
14
14
  - Machine: `EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD`.
15
- - Node.js `v12.16.2`
15
+ - Node.js `v16.9.1`
16
16
 
17
17
  ```
18
- FJS creation x 59,805 ops/sec ±0.23% (91 runs sampled)
18
+ FJS creation x 6,040 ops/sec ±1.17% (91 runs sampled)
19
19
 
20
- JSON.stringify array x 5,330 ops/sec ±0.54% (97 runs sampled)
21
- fast-json-stringify array x 6,995 ops/sec ±0.24% (94 runs sampled)
20
+ JSON.stringify array x 5,519 ops/sec ±0.08% (99 runs sampled)
21
+ fast-json-stringify array x 7,143 ops/sec ±0.14% (97 runs sampled)
22
22
 
23
- JSON.stringify long string x 15,108 ops/sec ±0.13% (100 runs sampled)
24
- fast-json-stringify long string x 15,089 ops/sec ±0.15% (98 runs sampled)
23
+ JSON.stringify long string x 16,438 ops/sec ±0.32% (98 runs sampled)
24
+ fast-json-stringify long string x 16,457 ops/sec ±0.09% (97 runs sampled)
25
25
 
26
- JSON.stringify short string x 13,214,696 ops/sec ±0.19% (97 runs sampled)
27
- fast-json-stringify short string x 33,378,500 ops/sec ±0.27% (95 runs sampled)
26
+ JSON.stringify short string x 12,061,258 ops/sec ±0.32% (97 runs sampled)
27
+ fast-json-stringify short string x 35,531,071 ops/sec ±0.17% (94 runs sampled)
28
28
 
29
- JSON.stringify obj x 3,172,653 ops/sec ±0.15% (98 runs sampled)
30
- fast-json-stringify obj x 13,537,123 ops/sec ±0.19% (95 runs sampled)
29
+ JSON.stringify obj x 3,079,746 ops/sec ±0.09% (95 runs sampled)
30
+ fast-json-stringify obj x 7,721,569 ops/sec ±0.12% (98 runs sampled)
31
+
32
+ JSON stringify date x 1,149,786 ops/sec ±0.10% (99 runs sampled)
33
+ fast-json-stringify date format x 1,674,498 ops/sec ±0.12% (99 runs sampled)
31
34
  ```
32
35
 
33
36
  #### Table of contents:
package/bench.js CHANGED
@@ -50,22 +50,38 @@ const arraySchemaCJS = {
50
50
  items: schemaCJS
51
51
  }
52
52
 
53
+ const dateFormatSchema = {
54
+ description: 'Date of birth',
55
+ type: 'string',
56
+ format: 'date'
57
+ }
58
+
59
+ const dateFormatSchemaCJS = {
60
+ description: 'Date of birth',
61
+ type: 'string',
62
+ format: 'date'
63
+ }
64
+
53
65
  const obj = {
54
66
  firstName: 'Matteo',
55
67
  lastName: 'Collina',
56
68
  age: 32
57
69
  }
58
70
 
71
+ const date = new Date()
72
+
59
73
  const multiArray = []
60
74
 
61
75
  const CJS = require('compile-json-stringify')
62
76
  const CJSStringify = CJS(schemaCJS)
63
77
  const CJSStringifyArray = CJS(arraySchemaCJS)
78
+ const CJSStringifyDate = CJS(dateFormatSchemaCJS)
64
79
  const CJSStringifyString = CJS({ type: 'string' })
65
80
 
66
81
  const FJS = require('.')
67
82
  const stringify = FJS(schema)
68
83
  const stringifyArray = FJS(arraySchema)
84
+ const stringifyDate = FJS(dateFormatSchema)
69
85
  const stringifyString = FJS({ type: 'string' })
70
86
  let str = ''
71
87
 
@@ -138,6 +154,18 @@ suite.add('compile-json-stringify obj', function () {
138
154
  CJSStringify(obj)
139
155
  })
140
156
 
157
+ suite.add('JSON stringify date', function () {
158
+ JSON.stringify(date)
159
+ })
160
+
161
+ suite.add('fast-json-stringify date format', function () {
162
+ stringifyDate(date)
163
+ })
164
+
165
+ suite.add('compile-json-stringify date format', function () {
166
+ CJSStringifyDate(date)
167
+ })
168
+
141
169
  suite.on('cycle', cycle)
142
170
 
143
171
  suite.run()
package/index.js CHANGED
@@ -285,10 +285,7 @@ function $asDatetime (date, skipQuotes) {
285
285
  function $asDate (date, skipQuotes) {
286
286
  const quotes = skipQuotes === true ? '' : '"'
287
287
  if (date instanceof Date) {
288
- const year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date)
289
- const month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(date)
290
- const day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date)
291
- return quotes + year + '-' + month + '-' + day + quotes
288
+ return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000 )).toISOString().slice(0, 10) + quotes
292
289
  } else if (date && typeof date.format === 'function') {
293
290
  return quotes + date.format('YYYY-MM-DD') + quotes
294
291
  } else {
@@ -959,9 +956,6 @@ function buildArray (location, code, name, key = null) {
959
956
  }
960
957
  `
961
958
  }
962
- code += `
963
- var json = '['
964
- `
965
959
  const laterCode = ''
966
960
 
967
961
  // default to any items type
@@ -1023,17 +1017,17 @@ function buildArray (location, code, name, key = null) {
1023
1017
 
1024
1018
  code += `
1025
1019
  var l = obj.length
1026
-
1020
+ var jsonOutput= ''
1027
1021
  for (var i = 0; i < l; i++) {
1028
- var jsonLastChar = json[json.length - 1]
1022
+ var json = ''
1023
+ ${result.code}
1024
+ jsonOutput += json
1029
1025
 
1030
- if (i > 0 && jsonLastChar !== '[' && jsonLastChar !== ',') {
1031
- json += ','
1026
+ if (json.length > 0 && i < l - 1) {
1027
+ jsonOutput += ','
1032
1028
  }
1033
- ${result.code}
1034
1029
  }
1035
- json += ']'
1036
- return json
1030
+ return \`[\${jsonOutput}]\`
1037
1031
  }
1038
1032
  ${result.laterCode}
1039
1033
  `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "2.7.9",
3
+ "version": "2.7.10",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",