The fastest way to repeat an array multiple times while deleting items

Imagine I have an array of numbers as follows:

var myArray = [1, 2, 3, 4, 5] ;

This is a simplification, my actual scenario is much larger (60K or more items).

Basically I need to iterate through the array completely in order to process several numbers, and then continue iterating through the array until all numbers are processed (say, I can process 1 and 3 at the first iteration and 2, 4 and 5 at second). EDIT: They don't have to be in sequential order.

I wonder: I need to make sure that elements that have already been processed in previous iterations will no longer be counted .

My question is: what is the fastest way to do this, every ms is counted because it is done on a large scale.

I tried to remove the items that were processed by doing myArray = myArray.splice(i, 1) , but this is very slow. I also tried setting the processed elements to null and then ignoring the null elements in subsequent iterations. This method works faster, but still takes a second or two. Is there a faster method?

+5
source share
2 answers

You can just save the index you got in the previous run. In the end, you do not want to repeat the iteration of the array several times, you want to repeat it only once, but in a few steps.

 function process(arr, step, start) { var take = Math.min(arr.length - start, Math.round(1 + Math.random() * 3)); for (var i = start; i < start + take; i++) { console.log("processing item " + arr[i] + " in step " + step); } return take; } var arr = [1, 2, 3, 4, 5] for (var step = 0, i = 0; i < arr.length; step++) i += process(arr, step, i); 
0
source
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { getColorGroup(); }); function getColorGroup() { var colors = ['#FAEBD7', '#00FFFF', '#000000', '#0000FF', '#8A2BE2', '#A52A2A', '#DEB887', '#5F9EA0', '#7FFF00', '#D2691E', '#FF7F50', '#6495ED', '#DC143C', '#00FFFF', '#00008B', '#008B8B', '#B8860B', '#A9A9A9', '#A9A9A9', '#006400', '#BDB76B', '#8B008B', '#556B2F', '#FF8C00', '#9932CC', '#8B0000', '#E9967A', '#8FBC8F', '#483D8B', '#2F4F4F', '#2F4F4F', '#00CED1', '#9400D3', '#FF1493', '#00BFFF', '#696969', '#696969', '#1E90FF', '#B22222', '#228B22', '#FF00FF', '#DCDCDC', '#FFD700', '#DAA520', '#808080', '#808080', '#008000', '#ADFF2F', '#FF69B4', '#CD5C5C', '#4B0082', '#F0E68C', '#E6E6FA', '#7CFC00', '#00FF00', '#32CD32', '#FF00FF', '#800000', '#66CDAA', '#0000CD', '#BA55D3', '#9370DB', '#3CB371', '#7B68EE', '#00FA9A', '#48D1CC', '#C71585', '#191970', '#FFE4E1', '#FFE4B5', '#FFDEAD', '#000080', '#808000', '#6B8E23', '#FFA500', '#FF4500', '#DA70D6', '#EEE8AA', '#98FB98', '#AFEEEE', '#DB7093', '#FFDAB9', '#CD853F', '#FFC0CB', '#DDA0DD', '#B0E0E6', '#800080', '#663399', '#FF0000', '#BC8F8F', '#4169E1', '#8B4513', '#FA8072', '#F4A460', '#2E8B57', '#A0522D', '#C0C0C0', '#87CEEB', '#6A5ACD', '#708090', '#708090', '#00FF7F', '#4682B4', '#D2B48C', '#008080', '#D8BFD8', '#FF6347', '#40E0D0', '#EE82EE', '#F5DEB3', '#FFFF00', '#9ACD32']; var randomIndex = Math.floor(Math.random() * 1000 % colors.length); var color = colors[randomIndex]; console.log(color); var tasks = []; for (var i = 0; i < colors.length; i += 10) { tasks.push(findColor(color, colors, i, 10)); } var group = []; $.when.apply($, tasks).done(function () { for(var j = 0; j < arguments.length; j++) { group = group.concat(arguments[j]); } console.log(group); }); } function findColor(sample, colors, startIndex, segmentSize) { return $.Deferred(function (deferred) { setTimeout(function(){ var result = []; var pattern = /#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/i; var sampleMatch = pattern.exec(sample); if (sampleMatch) { for (var i = startIndex; i < startIndex + segmentSize && i < colors.length; i++) { var match = pattern.exec(colors[i]); if (match) { if (sampleMatch[1].toUpperCase() == match[1].toUpperCase() || sampleMatch[2].toUpperCase() == match[2].toUpperCase() || sampleMatch[3].toUpperCase() == match[3].toUpperCase()) { result.push(colors[i]); } } } } deferred.resolve(result); }, 0); }).promise(); } </script> 
-2
source

Source: https://habr.com/ru/post/1258190/


All Articles