TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript Benchmark: performance.now

Perform a benchmark on a for-loop and measure the elapsed time with performance.now.
Benchmark, performance.now. Modern browsers deliver ever-increasing performance to JavaScript programs. Advanced optimizations are done on code.
For a developer, it is hard to know what method is the fastest one. For the development of any application, some benchmarks may be needed. Performance.now enables these.
Example benchmark. This is a benchmark harness page. It contains two calls to performance.now—these measure the current time in milliseconds.

And: We call performance.now once before, and once after, the code we wish to benchmark.

Result: We compute the elapsed time and display it in the title bar of the web browser (which is often a tab bar).

JavaScript program that uses performance.now // The performance.now method measures time in milliseconds. var time1 = performance.now(); // Perform the benchmark. for (var i = 0; i < 100000; i++) { var test = document.createElement("span"); test.id = "test"; } // Get end time and compute elapsed milliseconds. var time2 = performance.now(); var elapsed = time2 - time1; // Write elapsed time to browser title bar. document.title = elapsed + " ms"; Output 43.48 ms
Some notes. Different methods may perform slower or faster in different JavaScript engines. This is unavoidable. But the "ideal" JS code probably does well in all modern engines.
A summary. How much benchmarking should we do? Probably the more things we benchmark, the faster our code will become. But higher-level concerns, like page weight, are often more important.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf