Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rdtsc

NikolayMakhonin117Unlimited Free3.0.4TypeScript support: included

The most high resolution cross platform timing in NodeJs that use processor command RDTSC. Calculate performance with accuracy +/- 4 processor cycles.

native, high-resolution, performance, timing, rdtsc, benchmark, processor-cycles, tick, stopwatch, duration, thread-priority, process-priority, cross-platform

readme

NPM Version NPM Downloads Build Status Test Coverage

Description

The most high resolution timing in NodeJs

This module provide cross platform function rdtsc().

rdtsc() return counts the number of cycles since computer start

If you run in on Windows it will call C++ function _rdtsc() that call processor command [RDTSC (Read Time Stamp Counter)](https://en.wikipedia.org/wiki/TimeStamp_Counter)

(rdtsc() - rdtsc()) is always < 0

Notice: you should not use parallel threads since you calc performance

Install

npm i rdtsc -S

Features

RDTSC

var { rdtsc } = require('rdtsc')
console.log(rdtsc()) // 3864063236708616 cycles
console.log(rdtsc() - rdtsc()) // -2971 cycles, minimum = -130 cycles

Calc Performance

Accuracy: +/- 4 cycles

Syntax

/*

calcPerformance(testTimeMilliseconds, ...funcs)

result = {
    calcInfo: { 
        iterationCycles: 413.22476287663505,
        iterations: 5622917,
        funcsCount: 3,
        testTime: 1000 // milliseconds
    },
    cycles: [ 54n, 54n, 55n ], // is BigInt
    absoluteDiff: [ 0, 0 ], // cycles[i] - cycles[0]
    relativeDiff: undefined, // absoluteDiff[i] / absoluteDiff[0]
}

*/

Examples

const { calcPerformance } = require('rdtsc')

calcPerformance(1000, () => {}, () => {}, () => {})

/*
result = {
    calcInfo: { 
        iterationCycles: 413.22476287663505,
        iterations: 5622917,
        funcsCount: 3,
        testTime: 1000 // milliseconds
    },
    cycles: [ 54n, 54n, 54n ],
    absoluteDiff: [ 0, 0 ],
    relativeDiff: undefined,
}

OR

result = {
    ...
    cycles: [ 54n, 55n, 54n ],
    absoluteDiff: [ 1, 0 ],
    relativeDiff: [ 0 ],
}

OR

result = {
    ...
    cycles: [ 54n, 58n, 52n ],
    absoluteDiff: [ 4, -2 ],
    relativeDiff: [ -0.5 ],
}
*/

calcPerformance(1000, () => {}, () => {})

/*
result = {
    ...
    cycles: [ 54n, 54n ],
    absoluteDiff: [ 0 ],
    relativeDiff: undefined,
}
*/

calcPerformance(1000, () => {})

/*
result = {
    ...
    cycles: [ 54n ],
    absoluteDiff: undefined,
    relativeDiff: undefined,
}
*/
var { calcPerformance } = require('rdtsc')

var result = calcPerformance(
    1000,
    () => {

    },
    () => {
        Object.keys(Math)
    }
)

console.log('"Object.keys(Math)" min cycles =', result.absoluteDiff[0]) // about 20-40 cycles

Thread Priority

Implemented only for Windows platform

Examples


const { runInRealtimePriority, getThreadPriority, getProcessPriority } = require('rdtsc')

runInRealtimePriority(() => {
    console.log('getThreadPriority = ', getThreadPriority()) // === THREAD_PRIORITY_REALTIME
    console.log('getProcessPriority = ', getProcessPriority()) // === PROCESS_PRIORITY_REALTIME
})
const { setThreadPriority, getThreadPriority, isWin, THREAD_PRIORITY_REALTIME } = require('rdtsc')

if (isWin) {
    console.log(getThreadPriority()) // === THREAD_PRIORITY_NORMAL
} else {
    console.log(getThreadPriority()) // === undefined
}

var previousPriority = setThreadPriority(THREAD_PRIORITY_REALTIME)

try {
    // <your code>
} finally {
    setThreadPriority(previousPriority)
}

Priorities list

/*

const THREAD_PRIORITY_IDLE = -15
const THREAD_PRIORITY_LOWEST = -2
const THREAD_PRIORITY_BELOW_NORMAL = -1
const THREAD_PRIORITY_NORMAL = 0
const THREAD_PRIORITY_ABOVE_NORMAL = 1
const THREAD_PRIORITY_HIGHEST = 2
const THREAD_PRIORITY_REALTIME = 15 // THREAD_PRIORITY_REALTIME

*/

Process Priority

Implemented only for Windows platform

Example

const { setProcessPriority, getProcessPriority, isWin, PROCESS_PRIORITY_REALTIME } = require('rdtsc')

if (isWin) {
    console.log(getProcessPriority()) // === PROCESS_PRIORITY_NORMAL
} else {
    console.log(getProcessPriority()) // === undefined
}

var previousPriority = setProcessPriority(PROCESS_PRIORITY_REALTIME)

try {
    // <your code>
} finally {
    setProcessPriority(previousPriority)
}

Priorities list

/*

const PROCESS_PRIORITY_IDLE = 0x00000040 // IDLE_PRIORITY_CLASS
const PROCESS_PRIORITY_BELOW_NORMAL = 0x00004000 // BELOW_NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_NORMAL = 0x00000020 // NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_ABOVE_NORMAL = 0x00008000 // ABOVE_NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_HIGHEST = 0x00000080 // HIGH_PRIORITY_CLASS
const PROCESS_PRIORITY_REALTIME = 0x00000100 // REALTIME_PRIORITY_CLASS

*/

License

Unlimited Free

changelog

3.0.0 / 2022-06-26

  • Big refactoring
  • Added TypeScript definitions
  • Added async functions
  • Improved tests

2.0.0 / 2018-12-12

  • Support node v12.4.0
  • Tested on node v10.9.0 - v12.10.0

1.3.1 / 2018-12-12

  • calcPerformance function can evaluate performance of many functions

1.2.2 / 2018-12-03

  • Improved calcPerformance function
  • Removed dependencies

1.2.1 / 2018-12-03

  • Removed rdtsc0, rdtsc1 functions
  • Improved calcPerformance function

1.1.7 / 2018-12-03

  • Changed minimal node version to 10.9.0

1.1.4 / 2018-12-03

  • Improved code quality

1.1.2 / 2018-12-02

  • Fix int64 type for support not windows platforms

1.1.1 / 2018-12-02

  • Change license to CC0-1.0

1.1.0 / 2018-12-02

  • Added function: rdtsc0, rdtsc1 for better quality resolution. Examlpe to use: rdtsc0();func();cycles=rdtsc1();
  • Improved calcPerformance function

1.0.6 / 2018-12-02

  • Added function: runInRealtimePriority

1.0.3 / 2018-12-02

  • Added functions: setProcessPriority, getProcessPriority
  • Improved calcPerformance function

1.0.1 / 2018-12-02

  • Added functions: setThreadPriority, getThreadPriority
  • Improved calcPerformance function