Skip to content

Commit

Permalink
improve perf of asString (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jul 26, 2023
1 parent 8d72614 commit 17bb4c2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
19 changes: 19 additions & 0 deletions benchmark/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function getRandomInt (max) {
return Math.floor(Math.random() * max)
}

let longSimpleString = ''
for (let i = 0; i < LONG_STRING_LENGTH; i++) {
longSimpleString += i
}

let longString = ''
for (let i = 0; i < LONG_STRING_LENGTH; i++) {
longString += i
Expand All @@ -42,6 +47,20 @@ const benchmarks = [
},
input: 'hello world'
},
{
name: 'short string with double quote',
schema: {
type: 'string'
},
input: 'hello " world'
},
{
name: 'long string without double quotes',
schema: {
type: 'string'
},
input: longSimpleString
},
{
name: 'long string',
schema: {
Expand Down
35 changes: 17 additions & 18 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ module.exports = class Serializer {
}
}

// Fast escape chars check
if (!STR_ESCAPE.test(str)) {
return '"' + str + '"'
} else if (str.length < 42) {
if (str.length < 42) {
return this.asStringSmall(str)
} else if (STR_ESCAPE.test(str) === false) {
return '"' + str + '"'
} else {
return JSON.stringify(str)
}
Expand All @@ -130,32 +129,32 @@ module.exports = class Serializer {
// 34 and 92 happens all the time, so we
// have a fast case for them
asStringSmall (str) {
const l = str.length
const len = str.length
let result = ''
let last = 0
let found = false
let surrogateFound = false
let last = -1
let point = 255

// eslint-disable-next-line
for (var i = 0; i < l && point >= 32; i++) {
for (var i = 0; i < len; i++) {
point = str.charCodeAt(i)
if (point < 32) {
return JSON.stringify(str)
}
if (point >= 0xD800 && point <= 0xDFFF) {
// The current character is a surrogate.
surrogateFound = true
return JSON.stringify(str)
}
if (point === 34 || point === 92) {
if (
point === 0x22 || // '"'
point === 0x5c // '\'
) {
last === -1 && (last = 0)
result += str.slice(last, i) + '\\'
last = i
found = true
}
}

if (!found) {
result = str
} else {
result += str.slice(last)
}
return ((point < 32) || (surrogateFound === true)) ? JSON.stringify(str) : '"' + result + '"'
return (last === -1 && ('"' + str + '"')) || ('"' + result + str.slice(last) + '"')
}

getState () {
Expand Down
34 changes: 33 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ test('patternProperties - throw on unknown type', (t) => {
}
})

test('render a single quote as JSON', (t) => {
test('render a double quote as JSON /1', (t) => {
t.plan(2)

const schema = {
Expand All @@ -360,6 +360,38 @@ test('render a single quote as JSON', (t) => {
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('render a double quote as JSON /2', (t) => {
t.plan(2)

const schema = {
type: 'string'
}
const toStringify = 'double quote " 2'

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)

t.equal(output, JSON.stringify(toStringify))
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('render a long string', (t) => {
t.plan(2)

const schema = {
type: 'string'
}
const toStringify = 'the Ultimate Question of Life, the Universe, and Everything.'

const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)

t.equal(output, JSON.stringify(toStringify))
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('returns JSON.stringify if schema type is boolean', t => {
t.plan(1)

Expand Down

0 comments on commit 17bb4c2

Please sign in to comment.