Skip to content

Use performance.now() instead of Date.now()... #3483

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions packages/pg-native/bench/leaks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const Client = require('../')
const async = require('async')

let performance
try {
// Support for node < 16.0.0
performance = require('perf_hooks').performance
} catch (e) {
// failback for node < 8.5.0
performance = { now: Date.now } // Fallback to Date.now
}

const loop = function () {
const client = new Client()

Expand Down Expand Up @@ -37,10 +46,10 @@ const loop = function () {

const ops = [connect, simpleQuery, paramsQuery, prepared, sync, end]

const start = Date.now()
const start = performance.now()
async.series(ops, function (err) {
if (err) throw err
console.log(Date.now() - start)
console.log(performance.now() - start)
setImmediate(loop)
})
}
Expand Down
13 changes: 11 additions & 2 deletions packages/pg-protocol/src/b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import { BufferReader } from './buffer-reader'

let performance: { now: () => number }
try {
// Support for node < 16.0.0
performance = require('perf_hooks').performance
} catch (e) {
// failback for node < 8.5.0
performance = { now: Date.now } // Fallback to Date.now
}

const LOOPS = 1000
let count = 0
const start = Date.now()
const start = performance.now()

const reader = new BufferReader()
const buffer = Buffer.from([33, 33, 33, 33, 33, 33, 33, 0])

const run = () => {
if (count > LOOPS) {
console.log(Date.now() - start)
console.log(performance.now() - start)
return
}
count++
Expand Down
17 changes: 13 additions & 4 deletions packages/pg/bench.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const pg = require('./lib')

let performance
try {
// Support for node < 16.0.0
performance = require('perf_hooks').performance
} catch (e) {
// failback for node < 8.5.0
performance = { now: () => Date.now() }
}

const params = {
text: 'select typname, typnamespace, typowner, typlen, typbyval, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray from pg_type where typtypmod = $1 and typisdefined = $2',
values: [-1, true],
Expand All @@ -23,13 +32,13 @@ const exec = async (client, q) => {
}

const bench = async (client, q, time) => {
const start = Date.now()
const start = performance.now()
let count = 0
// eslint-disable-next-line no-constant-condition
while (true) {
await exec(client, q)
count++
if (Date.now() - start > time) {
if (performance.now() - start > time) {
return count
}
}
Expand Down Expand Up @@ -77,9 +86,9 @@ const run = async () => {
values: ['test', Buffer.allocUnsafe(104857600)],
})
console.log('bytea warmup done')
const start = Date.now()
const start = performance.now()
const results = await client.query('SELECT * FROM buf')
const time = Date.now() - start
const time = performance.now() - start
console.log('bytea time:', time, 'ms')
console.log('bytea length:', results.rows[0].data.byteLength, 'bytes')
console.log('on my laptop best so far seen 1407ms and 104857600 bytes')
Expand Down