Skip to content

Commit a2b84c4

Browse files
committed
Use performance.now() instead of Date.now()...
* Wherever applicable (measuring performance, not time). * Failback to support node < 16.0.0 (perf_hook not globally exposed) * Failback to Date.now() for node < 8.5.0 ✔ Tests passed with node > 16.0.0 (22.12.0) ✕ Couldn't pass with node prior 16.0.0 but not due to this changes. https://nodejs.org/docs/latest-v8.x/api/perf_hooks.html#perf_hooks_performance_now https://w3c.github.io/hr-time/
1 parent 03642ab commit a2b84c4

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

packages/pg-native/bench/leaks.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const Client = require('../')
22
const async = require('async')
33

4+
let performance: { now: () => number };
5+
try {
6+
// Support for node < 16.0.0
7+
performance = require('perf_hooks').performance;
8+
} catch (e) {
9+
// failback for node < 8.5.0
10+
performance = { now: Date.now }; // Fallback to Date.now
11+
}
12+
413
const loop = function () {
514
const client = new Client()
615

@@ -37,10 +46,10 @@ const loop = function () {
3746

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

40-
const start = Date.now()
49+
const start = performance.now()
4150
async.series(ops, function (err) {
4251
if (err) throw err
43-
console.log(Date.now() - start)
52+
console.log(performance.now() - start)
4453
setImmediate(loop)
4554
})
4655
}

packages/pg-protocol/src/b.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

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

5+
let performance: { now: () => number };
6+
try {
7+
// Support for node < 16.0.0
8+
performance = require('perf_hooks').performance;
9+
} catch (e) {
10+
// failback for node < 8.5.0
11+
performance = { now: Date.now }; // Fallback to Date.now
12+
}
13+
514
const LOOPS = 1000
615
let count = 0
7-
const start = Date.now()
16+
const start = performance.now()
817

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

1221
const run = () => {
1322
if (count > LOOPS) {
14-
console.log(Date.now() - start)
23+
console.log(performance.now() - start)
1524
return
1625
}
1726
count++

packages/pg/bench.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
const pg = require('./lib')
22

3+
let performance;
4+
try {
5+
// Support for node < 16.0.0
6+
performance = require('perf_hooks').performance
7+
} catch (e) {
8+
// failback for node < 8.5.0
9+
performance = { now: () => Date.now() }
10+
};
11+
312
const params = {
413
text: 'select typname, typnamespace, typowner, typlen, typbyval, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray from pg_type where typtypmod = $1 and typisdefined = $2',
514
values: [-1, true],
@@ -23,13 +32,13 @@ const exec = async (client, q) => {
2332
}
2433

2534
const bench = async (client, q, time) => {
26-
const start = Date.now()
35+
const start = performance.now()
2736
let count = 0
2837
// eslint-disable-next-line no-constant-condition
2938
while (true) {
3039
await exec(client, q)
3140
count++
32-
if (Date.now() - start > time) {
41+
if (performance.now() - start > time) {
3342
return count
3443
}
3544
}
@@ -77,9 +86,9 @@ const run = async () => {
7786
values: ['test', Buffer.allocUnsafe(104857600)],
7887
})
7988
console.log('bytea warmup done')
80-
const start = Date.now()
89+
const start = performance.now()
8190
const results = await client.query('SELECT * FROM buf')
82-
const time = Date.now() - start
91+
const time = performance.now() - start
8392
console.log('bytea time:', time, 'ms')
8493
console.log('bytea length:', results.rows[0].data.byteLength, 'bytes')
8594
console.log('on my laptop best so far seen 1407ms and 104857600 bytes')

0 commit comments

Comments
 (0)