Skip to content

Commit 0e81122

Browse files
committed
Correctly escape as for the spec.
Updated benchmarks Fixes #13
1 parent 8a9aa2f commit 0e81122

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +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 x1-5 times faster than `JSON.stringify()`.
3+
__fast-json-stringify__ is x1-4 times faster than `JSON.stringify()`.
44
It is particularly suited if you are sending small JSON payloads, the
55
advantages reduces on large payloads.
66

77
Benchmarks:
88

99
```
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)
10+
JSON.stringify array x 3,679 ops/sec ±1.01% (85 runs sampled)
11+
fast-json-stringify array x 4,618 ops/sec ±1.64% (87 runs sampled)
12+
JSON.stringify long string x 13,303 ops/sec ±1.01% (89 runs sampled)
13+
fast-json-stringify long string x 13,489 ops/sec ±0.88% (90 runs sampled)
14+
JSON.stringify short string x 4,974,749 ops/sec ±1.14% (86 runs sampled)
15+
fast-json-stringify short string x 11,030,700 ops/sec ±0.82% (89 runs sampled)
16+
JSON.stringify obj x 1,774,593 ops/sec ±1.07% (90 runs sampled)
17+
fast-json-stringify obj x 4,976,369 ops/sec ±1.00% (89 runs sampled)
1818
```
1919

2020
#### Table of contents:

index.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function build (schema, options) {
1616
code += `
1717
${$asString.toString()}
1818
${$asStringSmall.toString()}
19-
${$asStringLong.toString()}
2019
${$asNumber.toString()}
2120
${$asNull.toString()}
2221
${$asBoolean.toString()}
@@ -89,35 +88,24 @@ function $asString (str) {
8988
if (str.length < 42) {
9089
return $asStringSmall(str)
9190
} else {
92-
return $asStringLong(str)
91+
return JSON.stringify(str)
9392
}
9493
}
9594

96-
function $asStringLong (str) {
97-
var result = ''
98-
var l = str.length
99-
var i
100-
101-
for (;(i = str.indexOf('"')) >= 0 && i < l;) {
102-
result += str.slice(0, i) + '\\"'
103-
str = str.slice(i + 1)
104-
l = str.length
105-
}
106-
107-
if (l > 0) {
108-
result += str
109-
}
110-
111-
return '"' + result + '"'
112-
}
113-
95+
// magically escape strings for json
96+
// relying on their charCodeAt
97+
// everything below 32 needs JSON.stringify()
98+
// 34 and 92 happens all the time, so we
99+
// have a fast case for them
114100
function $asStringSmall (str) {
115101
var result = ''
116102
var last = 0
117103
var l = str.length
118-
for (var i = 0; i < l; i++) {
119-
if (str[i] === '"') {
120-
result += str.slice(last, i) + '\\"'
104+
var point = 255
105+
for (var i = 0; i < l && point >= 32; i++) {
106+
point = str.charCodeAt(i)
107+
if (point === 34 || point === 92) {
108+
result += str.slice(last, i) + '\\' + str[i]
121109
last = i + 1
122110
}
123111
}
@@ -126,7 +114,7 @@ function $asStringSmall (str) {
126114
} else {
127115
result += str.slice(last)
128116
}
129-
return '"' + result + '"'
117+
return point < 32 ? JSON.stringify(str) : '"' + result + '"'
130118
}
131119

132120
function $asRegExp (reg) {

test/basic.test.js

Lines changed: 5 additions & 0 deletions
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',
55+
type: 'string'
56+
}, 'hello\nworld')
57+
5358
buildTest({
5459
title: 'string with quotes',
5560
type: 'string'

0 commit comments

Comments
 (0)