Skip to content

Commit d9b0821

Browse files
committed
Better benchmark and handling of strings.
1 parent 55b3c4b commit d9b0821

File tree

4 files changed

+91
-46
lines changed

4 files changed

+91
-46
lines changed

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# fast-json-stringify  [![Build Status](https://travis-ci.org/mcollina/fast-json-stringify.svg)](https://travis-ci.org/mcollina/fast-json-stringify)
22

3-
__fast-json-stringify__ is x5 faster than `JSON.stringify()`.
3+
__fast-json-stringify__ is x1-5 times faster than `JSON.stringify()`.
4+
It is particularly suited if you are sending small JSON payloads, the
5+
advantages reduces on large payloads.
46

57
Benchmarks:
68

79
```
8-
JSON.stringify x 1,681,132 ops/sec ±0.59% (88 runs sampled)
9-
fast-json-stringify x 5,117,658 ops/sec ±1.44% (87 runs sampled)
10+
JSON.stringify array x 3,500 ops/sec ±0.91% (85 runs sampled)
11+
fast-json-stringify array x 4,456 ops/sec ±1.68% (87 runs sampled)
12+
JSON.stringify long string x 13,395 ops/sec ±0.88% (91 runs sampled)
13+
fast-json-stringify long string x 95,488 ops/sec ±1.04% (90 runs sampled)
14+
JSON.stringify short string x 5,059,316 ops/sec ±0.86% (92 runs sampled)
15+
fast-json-stringify short string x 12,219,967 ops/sec ±1.16% (91 runs sampled)
16+
JSON.stringify obj x 1,763,980 ops/sec ±1.30% (88 runs sampled)
17+
fast-json-stringify obj x 5,085,148 ops/sec ±1.56% (89 runs sampled)
1018
```
1119

1220
## Example

bench.js

+38-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const benchmark = require('benchmark')
4-
const safeStringify = require('fast-safe-stringify')
54
const suite = new benchmark.Suite()
65

76
const 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

5338
const stringify = require('.')(schema)
5439
const 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

6856
suite.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
}

index.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function build (schema) {
66
'use strict'
77
88
${$asString.toString()}
9+
${$asStringSmall.toString()}
10+
${$asStringLong.toString()}
911
${$asNumber.toString()}
1012
${$asNull.toString()}
1113
${$asBoolean.toString()}
@@ -65,27 +67,51 @@ function $asBoolean (bool) {
6567

6668
function $asString (str) {
6769
if (str instanceof Date) {
68-
str = str.toISOString()
69-
} else {
70+
return '"' + str.toISOString() + '"'
71+
} else if (typeof str !== 'string') {
7072
str = str.toString()
7173
}
7274

75+
if (str.length < 42) {
76+
return $asStringSmall(str)
77+
} else {
78+
return $asStringLong(str)
79+
}
80+
}
81+
82+
function $asStringLong (str) {
7383
var result = ''
74-
var last = 0
7584
var l = str.length
7685
var i
7786

78-
while ((i = str.indexOf('"')) >= 0 && i < l) {
79-
result += str.slice(last, i) + '\\"'
80-
last = i + 1
87+
for (;(i = str.indexOf('"')) >= 0 && i < l;) {
88+
result += str.slice(0, i) + '\\"'
89+
str = str.slice(i + 1)
90+
l = str.length
91+
}
92+
93+
if (l > 0) {
94+
result += str
8195
}
8296

97+
return '"' + result + '"'
98+
}
99+
100+
function $asStringSmall (str) {
101+
var result = ''
102+
var last = 0
103+
var l = str.length
104+
for (var i = 0; i < l; i++) {
105+
if (str[i] === '"') {
106+
result += str.slice(last, i) + '\\"'
107+
last = i + 1
108+
}
109+
}
83110
if (last === 0) {
84111
result = str
85112
} else {
86113
result += str.slice(last)
87114
}
88-
89115
return '"' + result + '"'
90116
}
91117

@@ -134,9 +160,11 @@ function buildArray (schema, code, name) {
134160
const result = nested(laterCode, name, '[i]', schema.items)
135161

136162
code += `
137-
for (var i = 0; i < obj.length; i++) {
163+
const l = obj.length
164+
const w = l - 1
165+
for (var i = 0; i < l; i++) {
138166
${result.code}
139-
if (i < obj.length - 1) {
167+
if (i < w) {
140168
json += ','
141169
}
142170
}

test.js

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ buildTest({
5050
type: 'string'
5151
}, 'hello world')
5252

53+
buildTest({
54+
title: 'string with quotes',
55+
type: 'string'
56+
}, 'hello """" world')
57+
5358
buildTest({
5459
title: 'boolean true',
5560
type: 'boolean'

0 commit comments

Comments
 (0)