@stdlib/utils-timeit

Time a snippet.

https://github.com/stdlib-js/utils-timeit

Science Score: 54.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.4%) to scientific vocabulary

Keywords

bench benchmark clock javascript measure node node-js nodejs perf performance stdlib tic time timeit timer toc util utilities utility utils
Last synced: 4 months ago · JSON representation ·

Repository

Time a snippet.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
bench benchmark clock javascript measure node node-js nodejs perf performance stdlib tic time timeit timer toc util utilities utility utils
Created over 4 years ago · Last pushed 12 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

timeit

NPM version Build Status Coverage Status <!-- dependencies -->

Time a snippet.

## Installation ```bash npm install @stdlib/utils-timeit ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). - To use as a general utility for the command line, install the corresponding [CLI package][cli-section] globally. The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships. To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage ```javascript var timeit = require( '@stdlib/utils-timeit' ); ``` #### timeit( code, \[options,] clbk ) Times a snippet. ```javascript var code = 'var x = Math.pow( Math.random(), 3 );'; code += 'if ( x !== x ) {'; code += 'throw new Error( \'Something went wrong.\' );'; code += '}'; timeit( code, done ); function done( error, results ) { if ( error ) { throw error; } console.dir( results ); /* e.g., => { 'iterations': 1000000, 'repeats': 3, 'min': [0,135734733], // [seconds,nanoseconds] 'elapsed': 0.135734733, // seconds 'rate': 7367311.062526641, // iterations/second 'times': [ // raw timing results [0,145641393], [0,135734733], [0,140462721] ] } */ } ``` The function supports the following `options`: - **before**: setup code. Default: `""`. - **after**: cleanup code. Default: `""`. - **iterations**: number of iterations. If `null`, the number of iterations is determined by trying successive powers of `10` until the total time is at least `0.1` seconds. Default: `1e6`. - **repeats**: number of repeats. Default: `3`. - **asynchronous**: `boolean` indicating whether a snippet is asynchronous. Default: `false`. To perform any setup or initialization, provide setup code. ```javascript var setup = 'var randu = require( \'@stdlib/random-base-randu\' );'; setup += 'var pow = require( \'@stdlib/math-base-special-pow\' );'; var code = 'var x = pow( randu(), 3 );'; code += 'if ( x !== x ) {'; code += 'throw new Error( \'Something went wrong.\' );'; code += '}'; var opts = { 'before': setup }; timeit( code, opts, done ); function done( error, results ) { if ( error ) { throw error; } console.dir( results ); } ``` To perform any cleanup, provide cleanup code. ```javascript var setup = 'var randu = require( \'@stdlib/random-base-randu\' );'; setup += 'var hypot = require( \'@stdlib/math-base-special-hypot\' );'; var code = 'var h = hypot( randu()*10, randu()*10 );'; code += 'if ( h < 0 || h > 200 ) {'; code += 'throw new Error( \'Something went wrong.\' );'; code += '}'; var cleanup = 'if ( h !== h ) {'; cleanup += 'throw new Error( \'Something went wrong.\' );'; cleanup += '}'; var opts = { 'before': setup, 'after': cleanup }; timeit( code, opts, done ); function done( error, results ) { if ( error ) { throw error; } console.dir( results ); } ``` To time an asynchronous snippet, set the `asynchronous` option to `true`. ```javascript var code = 'var x = Math.pow( Math.random(), 3 );'; code += 'if ( x !== x ) {'; code += 'var err = new Error( \'Something went wrong.\' );'; code += 'next( err );'; code += '}'; code += 'process.nextTick( next );'; var opts = { 'iterations': 1e2, 'asynchronous': true }; timeit( code, opts, done ); function done( error, results ) { if ( error ) { throw error; } console.dir( results ); } ``` If `asynchronous` is `true`, the implementation assumes that `before`, `after`, and `code` snippets are **all** asynchronous. Accordingly, these snippets should invoke a `next( [error] )` callback once complete. For example, given the following snippet, ```javascript setTimeout( done, 0 ); function done( error ) { if ( error ) { return next( error ); } next(); } ``` the implementation wraps the snippet within a function having the following signature ```javascript function wrapped( state, next ) { setTimeout( done, 0 ); function done( error ) { if ( error ) { return next( error ); } next(); } } ``` The `state` parameter is simply an empty `{}` which allows the `before`, `after`, and `code` snippets to share state. ```javascript function before( state, next ) { state.counter = 0; } function code( state, next ) { setTimeout( done, 0 ); function done( error ) { if ( error ) { return next( error ); } state.counter += 1; next(); } } function after( state, next ) { var err; if ( state.counter !== state.counter ) { err = new Error( 'Something went wrong!' ); return next( err ); } next(); } ```
## Notes - Snippets **always** run in [strict mode][strict-mode]. - **Always** verify results. Doing so prevents the compiler from performing dead code elimination and other optimization techniques, which would render timing results meaningless. - Executed code is **not** sandboxed and has access to the global state. You are **strongly** advised **against** timing untrusted code. To time untrusted code, do so in an isolated environment (e.g., a separate process with restricted access to both global state and the host environment). - Wrapping asynchronous code **does** add overhead, but, in most cases, the overhead should be negligible compared to the execution cost of the timed snippet. - Ensure that, when `asynchronous` is `true`, the main `code` snippet is actually asynchronous. If a snippet releases the [zalgo][zalgo], an error complaining about exceeding the maximum call stack size is highly likely. - While many benchmark frameworks calculate various statistics over raw timing results (e.g., mean and standard deviation), do **not** do this. Instead, consider the fastest time an approximate lower bound for how fast an environment can execute a snippet. Slower times are more likely attributable to other processes interfering with timing accuracy rather than attributable to variability in JavaScript's speed. In which case, the minimum time is most likely the only result of interest. When considering all raw timing results, apply common sense rather than statistics.
## Examples ```javascript var join = require( 'path' ).join; var readFileSync = require( '@stdlib/fs-read-file' ).sync; var timeit = require( '@stdlib/utils-timeit' ); var before = readFileSync( join( __dirname, 'examples', 'before.txt' ), 'utf8' ); var code = readFileSync( join( __dirname, 'examples', 'code.txt' ), 'utf8' ); var opts = { 'iterations': 1e6, 'repeats': 5, 'before': before }; timeit( code, opts, done ); function done( error, results ) { if ( error ) { throw error; } console.dir( results ); } ```

## CLI
## Installation To use as a general utility, install the CLI package globally ```bash npm install -g @stdlib/utils-timeit-cli ```
### Usage ```text Usage: timeit [options] [] Options: -h, --help Print this message. -V, --version Print the package version. --iterations iter Number of iterations. --repeats repeats Number of repeats. Default: 3. --before setup Setup code. --after cleanup Cleanup code. --async Time asynchronous code. --format fmt Output format: pretty, csv, json. Default: pretty. ```
### Notes - When the output format is `csv`, the output consists of **only** raw timing results. - If not explicitly provided `--iterations`, the implementation tries successive powers of `10` until the total time is at least `0.1` seconds.
### Examples ```bash $ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000 iterations: 1000000 repeats: 3 iterations/s: 7261975.851461222 elapsed time: 0.13770357 sec lower bound: 0.13770357 usec/iteration ``` To output results as JSON, ```bash $ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000 --format json {"iterations":1000000,"repeats":3,"min":[0,132431806],"elapsed":0.132431806,"rate":7551056.1261997735,"times":[[0,142115140],[0,132431806],[0,134808376]]} ``` To output results as comma-separated values ([CSV][csv]), ```bash $ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000 --format csv seconds,nanoseconds 0,139365407 0,138033545 0,135175834 ``` To use as part of a pipeline, ```bash $ cat ./examples/code.txt | timeit --before "$(cat ./examples/before.txt)" --iterations 1000000 iterations: 1000000 repeats: 3 iterations/s: 7433536.674260073 elapsed time: 0.134525468 sec lower bound: 0.134525468 usec/iteration ```


## References - Chen, Jiahao, and Jarrett Revels. 2016. "Robust benchmarking in noisy environments." _CoRR_ abs/1608.04295 (August). .
* * * ## Notice This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

Citation (CITATION.cff)

cff-version: 1.2.0
title: stdlib
message: >-
  If you use this software, please cite it using the
  metadata from this file.

type: software

authors:
  - name: The Stdlib Authors
    url: https://github.com/stdlib-js/stdlib/graphs/contributors

repository-code: https://github.com/stdlib-js/stdlib
url: https://stdlib.io

abstract: |
  Standard library for JavaScript and Node.js.

keywords:
  - JavaScript
  - Node.js
  - TypeScript
  - standard library
  - scientific computing
  - numerical computing
  - statistical computing

license: Apache-2.0 AND BSL-1.0

date-released: 2016

GitHub Events

Total
  • Push event: 12
Last Year
  • Push event: 12

Committers

Last synced: 6 months ago

All Time
  • Total Commits: 50
  • Total Committers: 1
  • Avg Commits per committer: 50.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 2
  • Committers: 1
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 50
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • npm 12 last-month
  • Total dependent packages: 3
    (may contain duplicates)
  • Total dependent repositories: 0
    (may contain duplicates)
  • Total versions: 17
  • Total maintainers: 4
npmjs.org: @stdlib/utils-timeit

Time a snippet.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.3
    published almost 2 years ago
  • Versions: 12
  • Dependent Packages: 3
  • Dependent Repositories: 0
  • Downloads: 8 Last month
Rankings
Dependent packages count: 9.5%
Downloads: 13.7%
Average: 16.9%
Forks count: 17.4%
Stargazers count: 18.8%
Dependent repos count: 25.3%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 5 months ago
npmjs.org: @stdlib/utils-timeit-cli

Time a snippet.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.3
    published almost 2 years ago
  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 4 Last month
Rankings
Forks count: 15.9%
Stargazers count: 17.3%
Average: 31.0%
Dependent repos count: 37.4%
Dependent packages count: 53.5%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 5 months ago

Dependencies

package.json npm
  • @stdlib/assert-contains ^0.0.x development
  • @stdlib/assert-is-browser ^0.0.x development
  • @stdlib/assert-is-json ^0.0.x development
  • @stdlib/assert-is-object ^0.0.x development
  • @stdlib/assert-is-windows ^0.0.x development
  • @stdlib/bench ^0.0.x development
  • @stdlib/math-base-assert-is-nan ^0.0.x development
  • @stdlib/math-base-special-hypot ^0.0.x development
  • @stdlib/process-exec-path ^0.0.x development
  • @stdlib/random-base-randu ^0.0.x development
  • @stdlib/string-replace ^0.0.x development
  • @stdlib/string-right-trim ^0.0.x development
  • istanbul ^0.4.1 development
  • proxyquire ^2.0.0 development
  • tap-spec 5.x.x development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/assert-has-own-property ^0.0.x
  • @stdlib/assert-is-array ^0.0.x
  • @stdlib/assert-is-boolean ^0.0.x
  • @stdlib/assert-is-function ^0.0.x
  • @stdlib/assert-is-null ^0.0.x
  • @stdlib/assert-is-plain-object ^0.0.x
  • @stdlib/assert-is-positive-integer ^0.0.x
  • @stdlib/assert-is-string ^0.0.x
  • @stdlib/cli-ctor ^0.0.x
  • @stdlib/fs-read-file ^0.0.x
  • @stdlib/process-cwd ^0.0.x
  • @stdlib/process-read-stdin ^0.0.x
  • @stdlib/random-base-minstd-shuffle ^0.0.x
  • @stdlib/streams-node-stdin ^0.0.x
  • @stdlib/string-format ^0.0.x
  • @stdlib/time-tic ^0.0.x
  • @stdlib/time-toc ^0.0.x
  • @stdlib/utils-copy ^0.0.x
  • @stdlib/utils-define-property ^0.0.x
  • @stdlib/utils-next-tick ^0.0.x
  • @stdlib/utils-noop ^0.0.x
.github/workflows/benchmark.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request v3 composite
.github/workflows/examples.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • actions/upload-artifact v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/productionize.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • JS-DevTools/npm-publish v1 composite
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/test.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/test_bundles.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • denoland/setup-deno v1 composite
.github/workflows/test_coverage.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • codecov/codecov-action v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/test_install.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite