time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

browser.js (1959B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var getGlobal = require( '@stdlib/utils/global' );
     24 var isObject = require( '@stdlib/assert/is-object' );
     25 var modf = require( '@stdlib/math/base/special/modf' );
     26 var round = require( '@stdlib/math/base/special/round' );
     27 var now = require( './now.js' );
     28 
     29 
     30 // VARIABLES //
     31 
     32 var Global = getGlobal();
     33 var ts;
     34 var ns;
     35 
     36 if ( isObject( Global.performance ) ) {
     37 	ns = Global.performance;
     38 } else {
     39 	ns = {};
     40 }
     41 if ( ns.now ) {
     42 	ts = ns.now.bind( ns );
     43 } else if ( ns.mozNow ) {
     44 	ts = ns.mozNow.bind( ns );
     45 } else if ( ns.msNow ) {
     46 	ts = ns.msNow.bind( ns );
     47 } else if ( ns.oNow ) {
     48 	ts = ns.oNow.bind( ns );
     49 } else if ( ns.webkitNow ) {
     50 	ts = ns.webkitNow.bind( ns );
     51 } else {
     52 	ts = now;
     53 }
     54 
     55 
     56 // MAIN //
     57 
     58 /**
     59 * Returns a high-resolution time.
     60 *
     61 * ## Notes
     62 *
     63 * -   Output format: `[seconds, nanoseconds]`.
     64 *
     65 *
     66 * @private
     67 * @returns {NumberArray} high-resolution time
     68 *
     69 * @example
     70 * var t = tic();
     71 * // returns [<number>,<number>]
     72 */
     73 function tic() {
     74 	var parts;
     75 	var t;
     76 
     77 	// Get a millisecond timestamp and convert to seconds:
     78 	t = ts() / 1000;
     79 
     80 	// Decompose the timestamp into integer (seconds) and fractional parts:
     81 	parts = modf( t );
     82 
     83 	// Convert the fractional part to nanoseconds:
     84 	parts[ 1 ] = round( parts[1] * 1.0e9 );
     85 
     86 	// Return the high-resolution time:
     87 	return parts;
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = tic;