hopefully some optimizations

This commit is contained in:
Conduitry 2018-03-24 16:46:42 -04:00
parent 34bebebf9c
commit 51c7257302
3 changed files with 25 additions and 25 deletions

View File

@ -1,6 +1,10 @@
import { handlers, defaultHandler } from './handlers.js'
import { handlers, withDefaultHandler } from './handlers.js'
export let add = (...classes) => addCustom(...classes, defaultHandler)
export let add = (...classes) => {
for (let Class of classes) {
withDefaultHandler.add(Class.prototype)
}
}
export let addCustom = (...classes) => {
let handler = classes.pop()

View File

@ -1,30 +1,10 @@
import { compareSymbols } from './compareSymbols.js'
export let handlers = new Map()
export let withDefaultHandler = new Set([Array.prototype, Object.prototype, null])
let OBJECT_START = {}
let OBJECT_END = {}
let REGEXP = {}
let DATE = {}
let BUFFER = {}
export let defaultHandler = (obj, push, recurse) => {
push(OBJECT_START, Object.getPrototypeOf(obj))
for (let property of Object.getOwnPropertyNames(obj)
.sort()
.concat(Object.getOwnPropertySymbols(obj).sort(compareSymbols))) {
push(property)
recurse(obj[property])
}
push(OBJECT_END)
}
handlers.set(Array.prototype, defaultHandler)
handlers.set(Object.prototype, defaultHandler)
handlers.set(null, defaultHandler)
handlers.set(RegExp.prototype, (obj, push) => push(REGEXP, obj.toString()))
handlers.set(Date.prototype, (obj, push) => push(DATE, obj.getTime()))

View File

@ -1,11 +1,27 @@
import { handlers } from './handlers.js'
import { handlers, withDefaultHandler } from './handlers.js'
import { compareSymbols } from './compareSymbols.js'
let OBJECT_START = {}
let OBJECT_END = {}
let array = []
let push = array.push.bind(array)
let recurse = obj => {
if (typeof obj === 'object' && obj !== null) {
let handler = handlers.get(Object.getPrototypeOf(obj))
let prototype = Object.getPrototypeOf(obj)
if (withDefaultHandler.has(prototype)) {
push(OBJECT_START, prototype)
for (let property of Object.getOwnPropertyNames(obj)
.sort()
.concat(Object.getOwnPropertySymbols(obj).sort(compareSymbols))) {
push(property)
recurse(obj[property])
}
push(OBJECT_END)
return
}
let handler = handlers.get(prototype)
if (handler) {
handler(obj, push, recurse)
return