Skip to content

Commit 258408d

Browse files
authored
Merge pull request #14 from mcollina/fast-escape
Correctly escape as for the spec.
2 parents 8a9aa2f + 23a51aa commit 258408d

File tree

3 files changed

+19
-55
lines changed

3 files changed

+19
-55
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: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ function build (schema, options) {
1515
`
1616
code += `
1717
${$asString.toString()}
18-
${$asStringSmall.toString()}
19-
${$asStringLong.toString()}
2018
${$asNumber.toString()}
2119
${$asNull.toString()}
2220
${$asBoolean.toString()}
@@ -86,47 +84,7 @@ function $asString (str) {
8684
str = str.toString()
8785
}
8886

89-
if (str.length < 42) {
90-
return $asStringSmall(str)
91-
} else {
92-
return $asStringLong(str)
93-
}
94-
}
95-
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-
114-
function $asStringSmall (str) {
115-
var result = ''
116-
var last = 0
117-
var l = str.length
118-
for (var i = 0; i < l; i++) {
119-
if (str[i] === '"') {
120-
result += str.slice(last, i) + '\\"'
121-
last = i + 1
122-
}
123-
}
124-
if (last === 0) {
125-
result = str
126-
} else {
127-
result += str.slice(last)
128-
}
129-
return '"' + result + '"'
87+
return JSON.stringify(str)
13088
}
13189

13290
function $asRegExp (reg) {
@@ -306,12 +264,13 @@ function buildObject (schema, code, name, externalSchema) {
306264

307265
code += result.code
308266
laterCode = result.laterCode
309-
267+
/* eslint-disable no-useless-escape */
310268
if (i < a.length - 1) {
311269
code += `
312270
json += \',\'
313271
`
314272
}
273+
/* eslint-enable no-useless-escape */
315274

316275
if (schema.required && schema.required.indexOf(key) !== -1) {
317276
code += `
@@ -398,14 +357,14 @@ function nested (laterCode, name, key, schema, externalSchema) {
398357
`
399358
break
400359
case 'object':
401-
funcName = (name + key).replace(/[-.\[\]]/g, '')
360+
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
402361
laterCode = buildObject(schema, laterCode, funcName, externalSchema)
403362
code += `
404363
json += ${funcName}(obj${key})
405364
`
406365
break
407366
case 'array':
408-
funcName = (name + key).replace(/[-.\[\]]/g, '')
367+
funcName = (name + key).replace(/[-.\[\]]/g, '') // eslint-disable-line
409368
laterCode = buildArray(schema, laterCode, funcName, externalSchema)
410369
code += `
411370
json += ${funcName}(obj${key})

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)