


In-depth exploration of the efficiency issues and related optimization of for loops in JavaScript_javascript skills
Underscore.js library
How many loops have you written in one day (week)?
var i; for(i = 0; i < someArray.length; i++) { var someThing = someArray[i]; doSomeWorkOn(someThing); }
It's harmless, of course, but it's ugly and weird and not really something to complain about. But this way of writing is too banal.
var i, j; for(i = 0; i < someArray.length; i++) { var someThing = someArray[i]; for(j = 0; j < someThing.stuff.length; j++) { doSomeWorkOn(someThing.stuff[j]); } }
You're extending bad code, and before you throw a bunch of ifs, you're already insane.
I haven't written a loop in two years.
"What are you talking about?"
It's true, a bad joke. Actually not none (ok, I did write a few), because I don’t write loops and my code is easier to understand.
How?
_.each(someArray, function(someThing) { doSomeWorkOn(someThing); })
Or better yet:
_.each(someArray, doSomeWorkOn);
This is what underscorejs does. Clean, simple, readable, short, no intermediate variables, no tons of semicolons, simple and very elegant.
Here are some more examples.
var i, result = []; for(i = 0; i < someArray.length; i++) { var someThing = someArray[i]; // 打到这,我已经手疼了 if(someThing.isAwesome === true) { result.push(someArray[i]); } }
Again, a typical use case for wasting time using loops. Even though these sites promote anti-smoking and vegetarianism, I feel outraged when I see these codes. Look at the simple way to write it.
var result = _.filter(someArray, function(someThing) { return someThing.isAwesome === true; })
Like the name of filter in underscore, just 3 lines of code can give you a new array.
Or do you want to convert these arrays into another form?
var result = _.map(someArray, function(someThing) { return trasformTheThing(someThing); })
The above three examples are sufficient in daily life, but these functions are not enough for underscore to be put on the table.
var grandTotal = 0, somePercentage = 1.07, severalNumbers = [33, 54, 42], i; // don't forget to hoist those indices; for(i = 0; i < severalNumbers.length; i++) { var aNumber = severalNumbers[i]; grandTotal += aNumber * somePercentage; }
underscore version
var somePercentage = 1.07, severalNumbers = [33, 54, 42], grandTotal; grandTotal = _.reduce(severalNumbers, function(runningTotal, aNumber) { return runningTotal + (aNumber * somePercentage); }, 0)
This may seem a bit strange at first. I checked the documentation about reduce and found out about its existence. Since I refuse to use loops, it's my first choice. The above things are just an introduction. The underscorejs library also has a lot of awesome functions.
30 days no use cycle challenge.
Don’t use any loops for the next 30 days. If you see a bunch of nasty and rough stuff, replace them with each or map. Use a little more reducing.
You need to note that Underscore is the gateway to functional programming. A visible, invisible way. A good way to go.
OurJS Note* Modern browsers currently support each, filter, map, and reduce methods, but the underscore library can achieve compatibility with older versions of IE. The following is an example written using ES5 native methods:
[3,4,5,3,3].forEach(function(obj){ console.log(obj); }); [1,2,3,4,5].filter(function(obj){ return obj < 3 }); [9,8,5,2,3,4,5].map(function(obj){ return obj + 2; }); [1,2,3,4,5].reduce(function(pre, cur, idx, arr) { console.log(idx); //4 个循环: 2-5 return pre + cur; }); //15 //sort方法同样很有用 [9,8,5,2,3,4,5].sort(function(obj1, obj2){ return obj1 - obj2; });
for in and for loop
Someone pointed out that the efficiency of for in is much lower than that of for loop. Now let's test the efficiency of using for in, for loop and forEach in different browsers when processing large arrays.
At present, most open source software will cache the array length in the for loop, because the common view is that some browsers Array.length will recalculate the array length every time, so temporary variables are usually used to store the array length in advance, such as:
for (var idx = 0, len = testArray.length; idx < len; idx++) { //do sth. }
We will also test the performance difference between caching and no caching.
Also add a summation operation in each test loop to show that it is not an empty loop.
for (var idx = 0, len = testArray.length; idx < len; idx++) { //do sth. }
We will also test the performance difference between caching and no caching.
Also add a summation operation in each test loop to show that it is not an empty loop.
The test code is as follows, click Run to view
HTML code
<h4 id="browser"></h4> <table id="results" class="table"></table>
JavaScript code
function () { //准备测试数据, 有200万条数据的大数组 var testArray = [] , testObject = {} , idx , len = 2000000 , tmp = 0 , $results = $("#results") , $browser = $("#browser") ; $browser.html(navigator.userAgent); $results.html(''); for (var i = 0; i < len; i++) { var number = Math.random(); //若希望加快运算速度可使用取整:Math.random() * 10 | 0 testArray.push(number); testObject[i] = number; } $results.append('<tr><th>测试代码</th><th>计算结果</th><th>所需时间,毫秒</th></tr>'); //测试函数 var test = function(testFunc) { var startTime , endTime , result ; startTime = new Date(); tmp = 0; testFunc(); endTime = new Date(); //计算测试用例(Test Case)运行所需要的时间 result = endTime - startTime; $results.append('<tr><td><pre class="brush:php;toolbar:false">{0}
Run [Please wait a moment]
Test results
The test results may vary depending on the calculation. This is a summary of the test results of Firefox, Chrome, and IE running on my machine.
The following are several observed conclusions
- For in is much slower than for loop, at least 20 times slower in Chrome
- FF has optimized forEach (ES5), and its performance is better than for loop, but Chrome/IEn performance is poor
- FF/Chrome caching Array.length is a little slower than using it directly. Except for the latest version of IE, the performance improvement is minimal (this is very unexpected)
- In some cases, FF’s JS engine performance seems to be better than V8

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Lambda expression breaks out of the loop, specific code examples are needed. In programming, the loop structure is an important syntax that is often used. However, in certain circumstances, we may want to break out of the entire loop when a certain condition is met within the loop body, rather than just terminating the current loop iteration. At this time, the characteristics of lambda expressions can help us achieve the goal of jumping out of the loop. Lambda expression is a way to declare an anonymous function, which can define simple function logic internally. It is different from an ordinary function declaration,

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
