├── .npmignore ├── .gitignore ├── .travis.yml ├── test ├── mocha.opts ├── scripts │ ├── difference.coffee │ ├── initial-value.coffee │ ├── delayed-call.coffee │ └── delayed-require.coffee ├── test-typings.ts ├── scripts.coffee └── performance-now.coffee ├── .tm_properties ├── src ├── index.d.ts └── performance-now.coffee ├── license.txt ├── package.json └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /node_modules/ 3 | /lib/ 4 | npm-debug.log -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "6" 5 | - "4" 6 | - "0.12" 7 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require coffee-script/register 2 | --compilers coffee:coffee-script/register 3 | --reporter spec -------------------------------------------------------------------------------- /.tm_properties: -------------------------------------------------------------------------------- 1 | excludeDirectories = "{.git,node_modules}" 2 | excludeInFolderSearch = "{excludeDirectories,lib}" 3 | 4 | includeFiles = "{.gitignore,.npmignore,.travis.yml}" 5 | 6 | [ attr.untitled ] 7 | fileType = 'source.coffee' -------------------------------------------------------------------------------- /test/scripts/difference.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ./node_modules/.bin/coffee 2 | 3 | # Expected output is above 0.005 and below 0.07. 4 | 5 | now = require('../../lib/performance-now') 6 | console.log -(now() - now()).toFixed 3 7 | -------------------------------------------------------------------------------- /src/index.d.ts: -------------------------------------------------------------------------------- 1 | // This file describes the package to typescript. 2 | 3 | /** 4 | * Returns the number of milliseconds since the page was loaded (if browser) 5 | * or the node process was started. 6 | */ 7 | declare function now(): number; 8 | export = now; 9 | -------------------------------------------------------------------------------- /test/test-typings.ts: -------------------------------------------------------------------------------- 1 | // In real code, users will do 2 | // import now = require('performance-now'); 3 | import now = require('../'); 4 | 5 | // Declare a variable to have the type `number`. 6 | let num: number; 7 | 8 | // If the typescript compiler doesn't warn here, 9 | // the result of now() is a number. 10 | num = now() 11 | -------------------------------------------------------------------------------- /test/scripts/initial-value.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ./node_modules/.bin/coffee 2 | 3 | ### 4 | Expected output is a number above 100 and below 350. 5 | The time reported is relative to the time the node.js process was started 6 | this is approximately at `(Date.now() process.uptime() * 1000)` 7 | ### 8 | 9 | now = require '../../lib/performance-now' 10 | console.log now().toFixed 3 11 | -------------------------------------------------------------------------------- /test/scripts/delayed-call.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ./node_modules/.bin/coffee 2 | 3 | ### 4 | Expected output is a number above 350 and below 600. 5 | The time reported is relative to the time the node.js process was started 6 | this is approximately at `(Date.now() process.uptime() * 1000)` 7 | ### 8 | 9 | delay = require "call-delayed" 10 | now = require "../../lib/performance-now" 11 | delay 250, -> console.log now().toFixed 3 12 | -------------------------------------------------------------------------------- /test/scripts/delayed-require.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ./node_modules/.bin/coffee 2 | 3 | ### 4 | Expected output is a number above 350 and below 600. 5 | The time reported is relative to the time the node.js process was started 6 | this is approximately at `(Date.now() process.uptime() * 1000)` 7 | ### 8 | 9 | delay = require "call-delayed" 10 | delay 250, -> 11 | now = require "../../lib/performance-now" 12 | console.log now().toFixed 3 13 | -------------------------------------------------------------------------------- /src/performance-now.coffee: -------------------------------------------------------------------------------- 1 | if performance? and performance.now 2 | module.exports = -> performance.now() 3 | else if process? and process.hrtime 4 | module.exports = -> (getNanoSeconds() - nodeLoadTime) / 1e6 5 | hrtime = process.hrtime 6 | getNanoSeconds = -> 7 | hr = hrtime() 8 | hr[0] * 1e9 + hr[1] 9 | moduleLoadTime = getNanoSeconds() 10 | upTime = process.uptime() * 1e9 11 | nodeLoadTime = moduleLoadTime - upTime 12 | else if Date.now 13 | module.exports = -> Date.now() - loadTime 14 | loadTime = Date.now() 15 | else 16 | module.exports = -> new Date().getTime() - loadTime 17 | loadTime = new Date().getTime() 18 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Braveg1rl 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "performance-now", 3 | "description": "Implements performance.now (based on process.hrtime).", 4 | "keywords": [], 5 | "version": "2.1.0", 6 | "author": "Braveg1rl ", 7 | "license": "MIT", 8 | "homepage": "https://github.com/braveg1rl/performance-now", 9 | "bugs": "https://github.com/braveg1rl/performance-now/issues", 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/braveg1rl/performance-now.git" 13 | }, 14 | "private": false, 15 | "dependencies": {}, 16 | "devDependencies": { 17 | "bluebird": "^3.4.7", 18 | "call-delayed": "^1.0.0", 19 | "chai": "^3.5.0", 20 | "chai-increasing": "^1.2.0", 21 | "coffee-script": "~1.12.2", 22 | "mocha": "~3.2.0", 23 | "pre-commit": "^1.2.2", 24 | "typescript": "^2.1.6" 25 | }, 26 | "optionalDependencies": {}, 27 | "main": "lib/performance-now.js", 28 | "scripts": { 29 | "build": "mkdir -p lib && rm -rf lib/* && node_modules/.bin/coffee --compile -m --output lib/ src/", 30 | "prepublish": "npm test", 31 | "pretest": "npm run build", 32 | "test": "node_modules/.bin/mocha && tsc --noEmit test/test-typings.ts", 33 | "watch": "node_modules/.bin/coffee --watch --compile --output lib/ src/" 34 | }, 35 | "typings": "src/index.d.ts" 36 | } 37 | -------------------------------------------------------------------------------- /test/scripts.coffee: -------------------------------------------------------------------------------- 1 | Bluebird = require "bluebird" 2 | exec = require("child_process").execSync 3 | {assert} = require "chai" 4 | 5 | describe "scripts/initital-value.coffee (module.uptime(), expressed in milliseconds)", -> 6 | result = exec("./test/scripts/initial-value.coffee").toString().trim() 7 | it "printed #{result}", -> 8 | it "printed a value above 100", -> assert.isAbove result, 100 9 | it "printed a value below 350", -> assert.isBelow result, 350 10 | 11 | describe "scripts/delayed-require.coffee (sum of uptime and 250 ms delay`)", -> 12 | result = exec("./test/scripts/delayed-require.coffee").toString().trim() 13 | it "printed #{result}", -> 14 | it "printed a value above 350", -> assert.isAbove result, 350 15 | it "printed a value below 600", -> assert.isBelow result, 600 16 | 17 | describe "scripts/delayed-call.coffee (sum of uptime and 250 ms delay`)", -> 18 | result = exec("./test/scripts/delayed-call.coffee").toString().trim() 19 | it "printed #{result}", -> 20 | it "printed a value above 350", -> assert.isAbove result, 350 21 | it "printed a value below 600", -> assert.isBelow result, 600 22 | 23 | describe "scripts/difference.coffee", -> 24 | result = exec("./test/scripts/difference.coffee").toString().trim() 25 | it "printed #{result}", -> 26 | it "printed a value above 0.005", -> assert.isAbove result, 0.005 27 | it "printed a value below 0.07", -> assert.isBelow result, 0.07 28 | -------------------------------------------------------------------------------- /test/performance-now.coffee: -------------------------------------------------------------------------------- 1 | chai = require "chai" 2 | chai.use(require "chai-increasing") 3 | {assert,expect} = chai 4 | Bluebird = require "bluebird" 5 | 6 | now = require "../" 7 | 8 | getUptime = -> process.uptime() * 1e3 9 | 10 | describe "now", -> 11 | it "reported time differs at most 1ms from a freshly reported uptime", -> 12 | assert.isAtMost Math.abs(now()-getUptime()), 1 13 | 14 | it "two subsequent calls return an increasing number", -> 15 | assert.isBelow now(), now() 16 | 17 | it "has less than 10 microseconds overhead", -> 18 | assert.isBelow Math.abs(now() - now()), 0.010 19 | 20 | it "can be called 1 million times in under 1 second (averaging under 1 microsecond per call)", -> 21 | @timeout 1000 22 | now() for [0...1e6] 23 | undefined 24 | 25 | it "for 10,000 numbers, number n is never bigger than number n-1", -> 26 | stamps = (now() for [1...10000]) 27 | expect(stamps).to.be.increasing 28 | 29 | it "shows that at least 0.2 ms has passed after a timeout of 1 ms", -> 30 | earlier = now() 31 | Bluebird.resolve().delay(1).then -> assert.isAbove (now()-earlier), 0.2 32 | 33 | it "shows that at most 3 ms has passed after a timeout of 1 ms", -> 34 | earlier = now() 35 | Bluebird.resolve().delay(1).then -> assert.isBelow (now()-earlier), 3 36 | 37 | it "shows that at least 190ms ms has passed after a timeout of 200ms", -> 38 | earlier = now() 39 | Bluebird.resolve().delay(200).then -> assert.isAbove (now()-earlier), 190 40 | 41 | it "shows that at most 220 ms has passed after a timeout of 200ms", -> 42 | earlier = now() 43 | Bluebird.resolve().delay(200).then -> assert.isBelow (now()-earlier), 220 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # performance-now [![Build Status](https://travis-ci.org/braveg1rl/performance-now.png?branch=master)](https://travis-ci.org/braveg1rl/performance-now) [![Dependency Status](https://david-dm.org/braveg1rl/performance-now.png)](https://david-dm.org/braveg1rl/performance-now) 2 | 3 | Implements a function similar to `performance.now` (based on `process.hrtime`). 4 | 5 | Modern browsers have a `window.performance` object with - among others - a `now` method which gives time in milliseconds, but with sub-millisecond precision. This module offers the same function based on the Node.js native `process.hrtime` function. 6 | 7 | Using `process.hrtime` means that the reported time will be monotonically increasing, and not subject to clock-drift. 8 | 9 | According to the [High Resolution Time specification](http://www.w3.org/TR/hr-time/), the number of milliseconds reported by `performance.now` should be relative to the value of `performance.timing.navigationStart`. 10 | 11 | In the current version of the module (2.0) the reported time is relative to the time the current Node process has started (inferred from `process.uptime()`). 12 | 13 | Version 1.0 reported a different time. The reported time was relative to the time the module was loaded (i.e. the time it was first `require`d). If you need this functionality, version 1.0 is still available on NPM. 14 | 15 | ## Example usage 16 | 17 | ```javascript 18 | var now = require("performance-now") 19 | var start = now() 20 | var end = now() 21 | console.log(start.toFixed(3)) // the number of milliseconds the current node process is running 22 | console.log((start-end).toFixed(3)) // ~ 0.002 on my system 23 | ``` 24 | 25 | Running the now function two times right after each other yields a time difference of a few microseconds. Given this overhead, I think it's best to assume that the precision of intervals computed with this method is not higher than 10 microseconds, if you don't know the exact overhead on your own system. 26 | 27 | ## License 28 | 29 | performance-now is released under the [MIT License](http://opensource.org/licenses/MIT). 30 | Copyright (c) 2017 Braveg1rl 31 | --------------------------------------------------------------------------------