Public
Edited
Jan 12, 2024
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
harness = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/bench-harness@umd/browser.js' )
Insert cell
Insert cell
sin = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-sin@umd/browser.js' )
Insert cell
Insert cell
randu = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@umd/browser.js' )
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function bench( /* description, [options,] clbk */ ) {
var args;
var i;
// Copy input arguments to an array:
args = [];
for ( i = 0; i < arguments.length; i++ ) {
args.push( arguments[ i ] );
}
return new Promise( executor );
function executor( resolve, reject ) {
var results;
var stream;
var h;
results = '';
// Create a new benchmark harness:
h = harness.createHarness( onFinish );
// Create a stream for benchmark results:
stream = h.createStream();
// Listen for 'data' events (i.e., benchmark results):
stream.on( 'data', onResults );
// Run the provided benchmark:
h.apply( h, args );
function onResults( data ) {
results += data.toString();
}
function onFinish() {
// Once a benchmark finishes, close the harness:
h.close();
// Return the benchmark results:
resolve( results );
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
function benchmark1( b ) {
var y;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = sin( randu() );
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( y !== y ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
b1_results = bench( 'sin', benchmark1 )
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more