Improve performance of depth benchmark by 20%
This commit is contained in:
Родитель
14566524c1
Коммит
e7f9471a13
25
lib/tools.js
25
lib/tools.js
|
@ -45,7 +45,7 @@ function asString (str) {
|
|||
var found = false
|
||||
var point = 255
|
||||
const l = str.length
|
||||
if (l > 42) {
|
||||
if (l > 100) {
|
||||
return JSON.stringify(str)
|
||||
}
|
||||
for (var i = 0; i < l && point >= 32; i++) {
|
||||
|
@ -95,9 +95,26 @@ function asJson (obj, msg, num, time) {
|
|||
for (var key in obj) {
|
||||
value = obj[key]
|
||||
if ((notHasOwnProperty || obj.hasOwnProperty(key)) && value !== undefined) {
|
||||
value = (stringifiers[key] || stringify)(serializers[key] ? serializers[key](value) : value)
|
||||
if (value !== undefined) {
|
||||
data += ',"' + key + '":' + value
|
||||
value = serializers[key] ? serializers[key](value) : value
|
||||
|
||||
switch (typeof value) {
|
||||
case 'undefined':
|
||||
break
|
||||
case 'string':
|
||||
value = (stringifiers[key] || asString)(value)
|
||||
if (value !== undefined) {
|
||||
data += ',"' + key + '":' + value
|
||||
}
|
||||
break
|
||||
case 'number':
|
||||
case 'boolean':
|
||||
data += ',"' + key + '":' + value
|
||||
break
|
||||
default:
|
||||
value = (stringifiers[key] || stringify)(value)
|
||||
if (value !== undefined) {
|
||||
data += ',"' + key + '":' + value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
10
pino.js
10
pino.js
|
@ -60,7 +60,7 @@ function pino (...args) {
|
|||
|
||||
assertNoLevelCollisions(pino.levels, customLevels)
|
||||
|
||||
const stringify = safe ? stringifySafe : JSON.stringify
|
||||
const stringify = safe ? trySafe : JSON.stringify
|
||||
const stringifiers = redact ? redaction(redact, stringify) : {}
|
||||
const formatOpts = redact
|
||||
? {stringify: stringifiers[redactFmtSym]}
|
||||
|
@ -112,3 +112,11 @@ pino.version = version
|
|||
pino.LOG_VERSION = LOG_VERSION
|
||||
|
||||
module.exports = pino
|
||||
|
||||
function trySafe (obj) {
|
||||
try {
|
||||
return JSON.stringify(obj)
|
||||
} catch (_) {
|
||||
return stringifySafe(obj)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
const os = require('os')
|
||||
const { join } = require('path')
|
||||
const { readFileSync } = require('fs')
|
||||
const proxyquire = require('proxyquire')
|
||||
const { test } = require('tap')
|
||||
const { sink, check, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
@ -446,15 +445,13 @@ test('children with same names render in correct order', async ({is}) => {
|
|||
})
|
||||
|
||||
// https://github.com/pinojs/pino/pull/251 - use this.stringify
|
||||
test('when safe is true it should ONLY use `fast-safe-stringify`', async ({is}) => {
|
||||
var isFastSafeStringifyCalled = false
|
||||
const mockedPino = proxyquire('../', {
|
||||
'fast-safe-stringify' () {
|
||||
isFastSafeStringifyCalled = true
|
||||
return '{ "hello": "world" }'
|
||||
}
|
||||
})
|
||||
const instance = mockedPino({ safe: true }, sink())
|
||||
instance.info({ hello: 'world' })
|
||||
is(isFastSafeStringifyCalled, true)
|
||||
test('when safe is true it should ONLY use `fast-safe-stringify`', async ({deepEqual}) => {
|
||||
const stream = sink()
|
||||
const root = pino({ safe: true }, stream)
|
||||
// circular depth
|
||||
const obj = {}
|
||||
obj.a = obj
|
||||
root.info(obj)
|
||||
const { a } = await once(stream, 'data')
|
||||
deepEqual(a, { a: '[Circular]' })
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче