Key words: de-unique

This is an archaeological article, which was raised by Yubo in 2013 when he was optimizing SeaJs performance. The original article here talks about performance optimization from JS array rehash, and discusses the methodology of solving the problem dialectically with array rehash as the starting point. The core idea can be seen in the original article, this article only covers the optimal method of array deduplication and a few other methods.

The best way

Using the property that the object does not duplicate, first create an empty object and then iterate through the for loop to check if the array elements are duplicated.

let a = [0.1.1.2.3.4.4.4];
let result = [];
let obj = {};

for(let i of a){
    if(! obj[i]){ result.push(i); obj[i] =1; }}Object.keys(obj)
Copy the code

The above approach is fine both in terms of the amount of code and the time complexity of execution.

Best method 2

Higher versions of the runtime environment, using ES6’s Set structure to deactivate, easier to use:

let set = new Set([1.2.3.3.3.3]); // Convert an array to a set
console.log(set ); // set {1,2,3}
console.log([...set]) // set to array [1,2,3]
Copy the code

These two methods are sufficient for many scenarios, but the requirements are often not so “pure”, and the actual work requires that the array de-duplication algorithm can be combined with different algorithms to achieve a compound requirement.

Personally, I understand that code optimization is a process of internal optimization and external compatibility. Regardless of whether it is inward or outward, I understand that the following points can be gained by persisting in doing it:

  1. Train yourself on code optimization and review the process
  2. The optimal result realization idea is written as a flow line
  3. The process lines of multiple optimal results are compared horizontally to summarize common nodes for addressing the problem
  4. Form a set of their own methodology, the application of the same (have not studied the eight-part essay, do not criticize, routine things are desirable), using templates can save time, with the saved time to create is the most efficient.
  • This is a skill that must be developed to help you read code written by others.
  • Good framework code can learn more than piecemeal learning, just as good teachers make learning easier.

Uniq function in the optimal method 3-underscore

Provide a complete compatibility scheme, also can be understood as cohesion, external only provide a function. For example, uniq functions underscore (array, [isSorted], [iteratee]) can unduplicate not only data but also object types.

  1. How many different ways can you do array de-duplication
  2. Identify problems and optimize them properly
  3. How much knowledge is there in this question? For example, IE8 does not support indexOf (knowledge points can be clearly remembered and flexibly used only when combined with the actual situation), the time complexity of indexOf, or the implementation method of indexOf webKit

Enjoy the implementation of the UNIq function in underscore

   _.uniq = _.unique = function(array, isSorted, iteratee, context) {
    if (array == null) return [];
    if(! _.isboolean (isSorted)) {context = iteratee; iteratee = isSorted; isSorted =false; } /** ** here _.iteratee **function (key){
    *      return function(obj){
    *        returnobj[key]; *} ** * key is an iteratee (an object's property), using the closure **/if(iteratee ! = null) iteratee = _.iteratee(iteratee, context); var result = []; Var seen = []; var seen = [];for(var i = 0, length = array.length; i < length; i++) { var value = array[i]; // Current comparison valueif(isSorted) {// If I =0, or seen (previous value) does not equal the current value, add it to the de-arrayif(! i || seen ! == value) result.push(value); seen = value; // Save the current value for the next comparison}else if (iteratee) {
        var computed = iteratee(value, i, array);
        if(_.indexOf(seen, computed) < 0) { seen.push(computed); result.push(value); }}else if(_.indexOf(result, value) < 0) { result.push(value); }}return result;
  };
Copy the code

Further study of

Further performance optimization will come in C++, since webkit is written in C++.

Some people also start writing a underscore library for c++, which is an interesting idea. The STL of c++ is equivalent to the underscore library of js, or some people write a TypeScript version of the STL TSTL, which is fun!

C++ indexOf string indexOf

#include <iostream>
using namespace std;
int indexOf( char *source, char *what );

int main(a)
{
	int ret = indexOf( "123456"."23" );
	cout << ret; / / 1
	return(0);
}

int indexOf( char *source, char *what )
{
	int ret = 0;
	if ( *source )
	{
		int i = 0;
		for ( i = 0; i < what[i]; i++ )
		{
			if( source[i] ! = what[i] )break;
		}
		if ( what[i] == '\ 0' )
			return(0);
		if ( source[i] == what[0] )
			i -= 1;      /* Fix BUG */
		ret = indexOf( source + i + 1, what );
		if ( ret >= 0 )
			return(i + ret + 1);
		return(- 1);
	}else
		return(- 1);
}
Copy the code

As a front-end pick up C++ there’s a nice open class open class