-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
7,106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tests | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
# time-it-js | ||
Calculate the execution time of a function in JavaScript | ||
# @codewell/time-it-js | ||
|
||
Calculate the execution time of a function in JavaScript. | ||
Works with both sync and async functions. Always returns a promise. | ||
|
||
## Installation | ||
|
||
``` | ||
npm install @codewell/time-it | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```JavaScript | ||
import timeIt from '@codewell/time-it'; | ||
|
||
// Write examples here | ||
const slowMessage = (message) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(message); | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
const message = await timeIt(slowMessage, "Description")("Hello, World!") | ||
// Prints "Description: 1000ms" to the console | ||
// and returns the message => "Hello, World!" | ||
|
||
``` | ||
|
||
## Issues | ||
|
||
Please help by posting issues here on github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
if (process.env.NODE_ENV === "production") { | ||
module.exports = require("./lib/prod"); | ||
} else { | ||
module.exports = require("./lib/dev"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
|
||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
|
||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
|
||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
|
||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
|
||
// Function that times the execution of another funciton | ||
// Usage timeIt(foo, "Description of execution")(arg1, arg2, ...) | ||
var timeIt = (func, description) => /*#__PURE__*/_asyncToGenerator(function* () { | ||
console.time(description); | ||
|
||
try { | ||
return yield func(...arguments); // Wait for the result here | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
console.timeEnd(description); | ||
} | ||
}); | ||
|
||
module.exports = timeIt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
|
||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
|
||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
|
||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
|
||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
|
||
// Function that times the execution of another funciton | ||
// Usage timeIt(foo, "Description of execution")(arg1, arg2, ...) | ||
var timeIt = (func, description) => /*#__PURE__*/_asyncToGenerator(function* () { | ||
console.time(description); | ||
|
||
try { | ||
return yield func(...arguments); // Wait for the result here | ||
} catch (error) { | ||
throw error; | ||
} finally { | ||
console.timeEnd(description); | ||
} | ||
}); | ||
|
||
module.exports = timeIt; |
Oops, something went wrong.