Skip to content

Commit

Permalink
fast utc implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed May 12, 2018
1 parent 06872ae commit 11cb8ab
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
33 changes: 33 additions & 0 deletions benchmarks/time.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

var bench = require('fastbench')
var pino = require('../')

var epoch = pino({timestamp: pino.stdTimeFunctions.epochTime}, pino.destination('/dev/null'))
var unix = pino({timestamp: pino.stdTimeFunctions.unixTime}, pino.destination('/dev/null'))
var utc = pino({timestamp: pino.stdTimeFunctions.utcTime}, pino.destination('/dev/null'))

var max = 100

var run = bench([
function benchPinoEpochTime (cb) {
for (var i = 0; i < max; i++) {
epoch.info('hello world')
}
setImmediate(cb)
},
function benchPinoUnixTime (cb) {
for (var i = 0; i < max; i++) {
unix.info('hello world')
}
setImmediate(cb)
},
function benchPinoUtcTime (cb) {
for (var i = 0; i < max; i++) {
utc.info('hello world')
}
setImmediate(cb)
}
], 10000)

run(run)
9 changes: 8 additions & 1 deletion lib/time.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const fastDate = require('fast-date')

function nullTime () {
return ''
}
Expand All @@ -12,8 +14,13 @@ function unixTime () {
return ',"time":' + Math.round(Date.now() / 1000.0)
}

function utcTime () {
return ',"time": "' + fastDate() + '"'
}

module.exports = {
nullTime: nullTime,
epochTime: epochTime,
unixTime: unixTime
unixTime: unixTime,
utcTime: utcTime
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"zuul": "^3.11.1"
},
"dependencies": {
"fast-date": "^1.0.3",
"fast-json-parse": "^1.0.3",
"fast-safe-stringify": "^2.0.4",
"flatstr": "^1.0.5",
Expand Down
2 changes: 1 addition & 1 deletion pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var defaultOptions = {
safe: true,
name: undefined,
serializers: {},
timestamp: time.epochTime,
timestamp: time.utcTime,
level: 'info',
levelVal: undefined,
prettyPrint: false,
Expand Down
3 changes: 2 additions & 1 deletion test/timestamp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ var pino = require('../')
var sink = require('./helper').sink

test('pino exposes standard time functions', function (t) {
t.plan(4)
t.plan(5)
t.ok(pino.stdTimeFunctions)
t.ok(pino.stdTimeFunctions.epochTime)
t.ok(pino.stdTimeFunctions.unixTime)
t.ok(pino.stdTimeFunctions.nullTime)
t.ok(pino.stdTimeFunctions.utcTime)
})

test('pino accepts external time functions', function (t) {
Expand Down

0 comments on commit 11cb8ab

Please sign in to comment.