Fastest way to check two lines for exact match in JavaScript

I want to compare two lines in JavaScript to check if they are the same. What would be the best (fastest) way to do this?

Right now, I'm considering either

if(string1.localeCompare(string2) == 0) {} 

or simply

 if(string1 == string2) 

Is there a better way to do this?

+7
javascript string-matching
source share
4 answers

I would probably use strict equality if you want to verify that they are exactly the same, i.e. they are also of the same type, just in case.

 if (string1 === string2) 
+22
source share

Check this script * and find out which one is faster.

* In case the link will be in the future: == > === > String.localeCompare (tested in Chrome).

+4
source share

I'm not sure if there is an opportunity to optimize if(string1 == string2) . This is the best approach.

+1
source share
 if (typeof string1=="string" && typeof string2=="string" && string1 === string2) 

no evacuation method :)

+1
source share

All Articles