Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve perf of asString #632

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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