Skip to content

Commit

Permalink
Add core func and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fippli committed Apr 17, 2020
1 parent e12c901 commit bc51ce4
Show file tree
Hide file tree
Showing 11 changed files with 7,106 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,13 @@ dist

# TernJS port file
.tern-port
# Mac specific
.DS_Store

# Secret stuff
.npmrc
.env

# Node package
node_modules
*.tgz
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests
src
36 changes: 34 additions & 2 deletions README.md
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
6 changes: 6 additions & 0 deletions index.js
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");
}
53 changes: 53 additions & 0 deletions lib/dev.js
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;
53 changes: 53 additions & 0 deletions lib/prod.js
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;
Loading

0 comments on commit bc51ce4

Please sign in to comment.