{"id":15713169,"url":"https://github.com/timothygu/nanotimer","last_synced_at":"2025-03-30T18:29:00.522Z","repository":{"id":32566907,"uuid":"36149684","full_name":"TimothyGu/nanotimer","owner":"TimothyGu","description":null,"archived":false,"fork":false,"pushed_at":"2015-05-24T01:05:43.000Z","size":120,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T21:00:41.139Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TimothyGu.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-24T01:01:54.000Z","updated_at":"2015-05-24T01:02:10.000Z","dependencies_parsed_at":"2022-09-07T23:20:52.831Z","dependency_job_id":null,"html_url":"https://github.com/TimothyGu/nanotimer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyGu%2Fnanotimer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyGu%2Fnanotimer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyGu%2Fnanotimer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyGu%2Fnanotimer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimothyGu","download_url":"https://codeload.github.com/TimothyGu/nanotimer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246361935,"owners_count":20765006,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T21:21:40.496Z","updated_at":"2025-03-30T18:29:00.492Z","avatar_url":"https://github.com/TimothyGu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"nanotimer\n=========\n\n\u003e Nanosecond precision stopwatch for synchronous, asynchrous, and\n\u003e Promise-returning functions, using [`process.hrtime()`][1].\n\n[1]: https://nodejs.org/api/process.html#process_process_hrtime\n\n```js\nvar timer = require('nanotimer')\n```\n\nTime format\n-----------\n\nThe returned `time` property or variable has the following structure:\n\n```js\n   [       0,   123456789 ]\n// [ seconds, nanoseconds ]\n```\n\nTo convert this to seconds, do this:\n\n```js\nvar sec = time[0] + time[1] / 1e9\n```\n\nTo milliseconds:\n\n```js\nvar ms = time[0] * 1e3 + time[1] / 1e6\n```\n\nSynchronous function: `timer.sync`\n----------------------------------\n\n```js\ntimer.sync(function[, args]) =\u003e { result, time }\n```\n\nExample:\n\n```js\ntimer.sync(function () {\n  var i = 0\n  while (i \u003c 1000000000) i ++\n})\n// =\u003e { result: undefined, time: [ 1, 272903531 ] }\n\ntimer.sync(function (myArg) {\n  var i = 0\n  while (i \u003c 1000000000) i ++\n  return myArg + ' is cool'\n}, 'Timothy')\n// =\u003e { result: 'Timothy is cool', time: [ 1, 277721967 ] }\n```\n\nAsynchronous function with callbacks: `timer.async`\n---------------------------------------------------\n\n```js\nmyAsyncFunction(args,             function callback (usualArgs      ) )\n//                    ++++++++++++                            ++++++ +\nmyAsyncFunction(args, timer.async(function callback (usualArgs, time)))\n```\n\n`timer.async` wraps a callback, calling it with one more argument, `time`.\n\nExamples:\n\n```js\n// Old:\nfs.readFile('my-file', 'utf8',             function (err, data      ) {\n  if (err) return console.error('Eeeaash', err)\n  console.log('Your file is:')\n  console.log(data)\n})\n\n// New:\nfs.readFile('my-file', 'utf8', timer.async(function (err, data, time) {\n  console.log('Reading your file took', time[0] + time[1] / 1e9, 'seconds')\n  if (err) return console.error('Eeeaash', err)\n  console.log('Your file is:')\n  console.log(data)\n}))\n```\n\nAsynchronous function returning Promises: `timer.promise`\n-------------------------------------------\n\n```js\ntimer.promise(Promise) =\u003e Promise =\u003e { result/err, time }\n```\n\nTakes a Promise as argument, returning a Promise that either resolves to\n`{ result, time }`, with `result` the value of the original Promise, or\n`{ err, time }`, with `err` the reason for rejection of the original Promise.\n\nExamples:\n\n```js\ntimer.promise(new Promise(function (fulfill, reject) {\n  setTimeout(fulfill, 200, 1)\n})).then(console.log.bind(console))\n// =\u003e Prints { result: 1, time: [ 0, 204734279 ] }\n\ntimer.promise(new Promise(function (fulfill, reject) {\n  setTimeout(reject, 200, new Error('this is bad'))\n})).then(null, console.log.bind(console))\n// =\u003e Prints { err: [Error: this is bad], time: [ 0, 202782629 ] }\n```\n\nAsynchronous with Promise-returning functions: `timer.promiseFunc`\n------------------------------------------------------------------\n\n```js\ntimer.promiseFunc(function =\u003e Promise) =\u003e function =\u003e Promise =\u003e { result/err, time }\n```\n\nThis is a syntactic sugar for:\n\n```js\npromise.then(function (value) {\n  timer.promise(function () {\n    // do something with value\n    return anotherPromise\n  })\n}, function (err) {\n  timer.promise(function () {\n    // do something with err\n    return anotherPromise\n  })\n})\n```\n\nNow you can do:\n\n```js\npromise.then(timer.promiseFunc(function (value) {\n  // do something with value\n  return anotherPromise\n}), timer.promiseFunc(function (err) {\n  // do something with err\n  return anotherPromise\n}))\n```\n\nExamples:\n\n```js\n// In the following example, fs.readFileAsync returns a promise rather than\n// using callbacks. It is equivalent to the result of Bluebird's\n// `promisifyAll`.\n\nPromise.resolve('this-is-a-file')\n.then(timer.promiseFunc(fs.readFileAsync))\n.then(function (val) {\n  var result = val.result.toString()\n  var time   = val.time\n  // ...\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothygu%2Fnanotimer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimothygu%2Fnanotimer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothygu%2Fnanotimer/lists"}