Skip to content
Fix Code Error

How to measure time taken by a function to execute

March 13, 2021 by Code Error
Posted By: Jangwenyi

I need to get execution time in milliseconds.

I originally asked this question back in 2008. The accepted answer then was to use new Date().getTime() However, we can all agree now that using the standard performance.now() API is more appropriate. I am therefore changing the accepted answer to this one.

Solution

Using performance.now():

var t0 = performance.now()

doSomething()   // <---- The function you're measuring time for 

var t1 = performance.now()
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")

NodeJs: it is required to import the performance class


Using console.time: (non-standard) (living standard)

console.time('someFunction')

someFunction() // Whatever is timed goes between the two "console.time"

console.timeEnd('someFunction')

Note:
The string being pass to the time() and timeEnd() methods must match
(for the timer to finish as expected).

console.time() documentations:

  1. NodeJS documentation regarding
  2. MDN (client-side) documentation
Answered By: Anonymous

Related Articles

  • Gradle error: Execution failed for task…
  • org.gradle.api.tasks.TaskExecutionException:…
  • Backbone click event binding not being bound to DOM elements
  • Execution failed for task…
  • Obtain most recent value for based on index in a…
  • Android- Error:Execution failed for task…
  • Jetpack Compose and Hilt Conflict
  • How to set HTML5 required attribute in Javascript?
  • Gradle: Execution failed for task ':processDebugManifest'
  • When I'm testing a web app by JUnit and Mockito I…
  • Error:Execution failed for task…
  • How do I obtain a Query Execution Plan in SQL Server?
  • Exception : AAPT2 error: check logs for details
  • SMTP error 554
  • Notion APIs with Google Calendar
  • DAX Function Using Variables for Efficiency Purposes
  • Group array of objects by multiple keys using d3.groups
  • How to convert milliseconds to "hh:mm:ss" format?
  • How to convert java.util.Date to java.sql.Date?
  • How To Edit Date and Time Picker Vuejs Vuetify From…
  • Convert Java Date to UTC String
  • Examples of GoF Design Patterns in Java's core libraries
  • How to pass props to {this.props.children}
  • Spring: Why do we autowire the interface and not the…
  • D3 chart integration into Vuejs
  • Vue.js inheritance call parent method
  • How to change TIMEZONE for a java.util.Calendar/Date
  • Adding asterisk to required fields in Bootstrap 3
  • Best way to do multi-row insert in Oracle?
  • Show scroll update when scrolling down page
  • Persistent invalid graphics state error when using ggplot2
  • Getting the difference between two Dates…
  • QUnit, Sinon.js & Backbone unit test…
  • Custom date picker in Vuetify
  • Good way of getting the user's location in Android
  • How do I use namespaces with TypeScript external modules?
  • Convert Current date to integer
  • Java SimpleDateFormat for time zone with a colon separator?
  • How to print a date in a regular format?
  • Sort table rows In Bootstrap
  • Is this request generated by EF Core buggy or is it my code?
  • call a python function
  • The number of method references in a .dex file…
  • Beautiful way to remove GET-variables with PHP?
  • How do I set the conditional requirements and how do…
  • Vuetify - Rules should return a string or boolean,…
  • bootstrap-datepicker in dd/mm/yyyy - selected date…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Check synchronously if file/directory exists in Node.js
  • convert iso date to milliseconds in javascript
  • How to parse JSON file with Spring
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • What's the better (cleaner) way to ignore output in…
  • ClassNotFoundException thrown
  • Subtracting Dates in Oracle - Number or Interval Datatype?
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • How to obtain the start time and end time of a day?
  • Property or method "key" is not defined on the…
  • Cannot install packages using node package manager in Ubuntu
  • proper way to dynamically assign backbone.js view el
  • Logging best practices
  • Smart way to truncate long strings
  • List of Timezone IDs for use with FindTimeZoneById() in C#?
  • DataTable draw daterange from vaadin-date-picker in polymer
  • Print all day-dates between two dates
  • Get time difference between two dates in seconds
  • What is the scope of variables in JavaScript?
  • JSON string to JS object
  • Get Selected value from dropdown using JavaScript
  • Any difference between await Promise.all() and…
  • What's the difference between Instant and LocalDateTime?
  • Item position in RecyclerView only changing when…
  • Caching a jquery ajax response in javascript/browser
  • java.lang.RuntimeException:…
  • How to convert HH:mm:ss.SSS to milliseconds?
  • How do you get a timestamp in JavaScript?
  • Error: Can't set headers after they are sent to the client
  • Using query strings in backbone (1.0.0)
  • Multiple dates with javascript
  • what is the right emberjs way to switch between…
  • How to get the first day of the current week and month?
  • Pandas Dataframe Comparison and Copying
  • Compare two dates with JavaScript
  • Basic example of rendering a d3 SVG with a backbone View
  • How can I mock an ES6 module import using Jest?
  • Memcached vs. Redis?
  • Best practices for API versioning?
  • Access api from a CMS in Next.js
  • useEffect Error: Minified React error #321 (GTM…
  • Does moment.js allow me to derive a timezone…
  • How to resolve "Could not find schema information…
  • Aurelia - help understanding some binding behavior…
  • Calculate last day of month in JavaScript
  • How should I call 3 functions in order to execute…
  • JavaScript: How to reference the top/root of a Class?
  • Failed to execute goal…
  • Shuffle positions of options in flutter
  • Displaying the Dates that user have chosen from date input
  • Calculate the execution time of a method
  • Reference - What does this regex mean?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Count the frequency that a value occurs in a dataframe column

Next Post:

AngularJS: Service vs provider vs factory

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error