Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
It stringifies slower than v1 and JSON, but it parses faster than v1 and JSON :)
  • Loading branch information
wmertens committed Apr 2, 2017
1 parent ed7f6b6 commit 5986de0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
19 changes: 19 additions & 0 deletions examples/performance.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
<script>
const count = 200000
const v = { a: [ [1, 2], [], false, {}, true ], b: [], c: { d: 'hello', e: {}, f: [], g: true, n: null } }
const JSONParse = () => {
const n = Date.now()
const s = JSON.stringify(v)
for (let i = 0; i < count; i++) {
JSON.parse(s)
}
const ms = Date.now() - n
console.log(`JSON: ${count} parsed in ${ms}ms, ${ms / count}ms/item`)
}
const JSONStringify = () => {
const n = Date.now()
for (let i = 0; i < count; i++) {
JSON.stringify(v)
}
const ms = Date.now() - n
console.log(`JSON: ${count} stringified in ${ms}ms, ${ms / count}ms/item`)
}
const v1Parse = () => {
const n = Date.now()
const s = JSURL.stringify(v)
Expand Down Expand Up @@ -40,6 +57,8 @@
console.log(`v2: ${count} stringified in ${ms}ms, ${ms / count}ms/item`)
}
window.onload = () => {
JSONParse()
JSONStringify()
v1Parse()
v1Stringify()
v2Parse()
Expand Down
24 changes: 13 additions & 11 deletions lib/jsurl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@
}
var escapeRE = /(_|\*.)/g
function unescape (s) {
return s.replace(escapeRE, origChar)
// oddly enough, testing first is faster
return escapeRE.test(s) ? s.replace(escapeRE, origChar) : s
}
var replaceRE = /([*_~%$+'() ])/g
function escape (s) {
return s.replace(replaceRE, escCode)
// oddly enough, testing first is faster
return replaceRE.test(s) ? s.replace(replaceRE, escCode) : s
}
function eat (a) {
var j, c
Expand Down Expand Up @@ -125,20 +127,21 @@
} else {
throw new Error('Unknown dict reference', k)
}
} else if (c === '*') {
eatOne(a)
out = unescape(eat(a))
} else if (numRE.test(c)) {
out = Number(eat(a))
} else if (stringRE.test(c)) {
out = unescape(eat(a))
} else if (c === '*') {
eatOne(a)
out = unescape(eat(a))
} else {
throw new Error('Cannot decode part ' + [t].concat(a).join('~'))
}
return out
}

var endTildesRE = /~*$/
var TRUEt = TRUE + '~'
function encode (v) {
var t, a, out, T = typeof v, val

Expand All @@ -161,20 +164,19 @@
} else if (Array.isArray(v)) {
a = []
for (var i = 0; i < v.length; i++) {
t = encode(v[i])
t = v[i]
// Special case: only use full -T~ in arrays
a[i] = (t === '~' ? TRUE + '~' : t) || NULL
a[i] = t === true ? TRUEt : encode(t)
}

out = '!' + a.join('')
} else {
a = []
for (var key in v) {
if (v.hasOwnProperty(key)) {
val = encode(v[key])

// skip undefined and functions
if (val !== UNDEF + '~') {
t = v[key]
if (t !== undefined && typeof t !== 'function') {
val = encode(t)
a.push(escape(key) + '~', val)
}
}
Expand Down

0 comments on commit 5986de0

Please sign in to comment.