Skip to content

Commit 7829a09

Browse files
authored
Moar speed in arrays (#359)
* Improve rendering speed of arrays over JSON.stringify() This restore some perf that got lost along the way, moving from 4k ops/sec to 7k ops/sec in our array bench. * Bench updates
1 parent 9a971f3 commit 7829a09

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ __fast-json-stringify__ is significantly faster than `JSON.stringify()` for smal
1212
##### Benchmarks
1313

1414
- Machine: `EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD`.
15-
- Node.js `v12.16.2`
15+
- Node.js `v16.9.1`
1616

1717
```
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)
1919
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)
2222
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)
2525
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)
2828
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)
3134
```
3235

3336
#### Table of contents:

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,17 +1017,15 @@ function buildArray (location, code, name, key = null) {
10171017

10181018
code += `
10191019
var l = obj.length
1020-
var jsonOutput=''
1021-
var jsonLastChar
1020+
var jsonOutput= ''
10221021
for (var i = 0; i < l; i++) {
10231022
var json = ''
1024-
1025-
if (jsonLastChar && jsonLastChar.length && jsonLastChar !== ',') {
1026-
json += ','
1027-
}
10281023
${result.code}
1029-
jsonLastChar = json.slice(-1)
10301024
jsonOutput += json
1025+
1026+
if (json.length > 0 && i < l - 1) {
1027+
jsonOutput += ','
1028+
}
10311029
}
10321030
return \`[\${jsonOutput}]\`
10331031
}

0 commit comments

Comments
 (0)