localeCompare() vs .Sort for alphabetically sorting [duplicate] - javascript

This question already has answers here:
400x Sorting Speedup by Switching a.localeCompare(b) to (a<b?-1:(a>b?1:0))
(5 answers)
Closed 3 years ago.
Im sorting an array of objects alphabetically but not sure which way is more efficient. Im currently using the .sort() method and its working fine but would using the localeCompare() be a better alternative?

The localeCompare function is absurdly slow on many browser. Avoid it if possible. The other locale functions are really bad too, particularly the number to string ones.

Related

How to get all possible combinations of an array but with certain elements only changing (in Javascript) [duplicate]

This question already has answers here:
Cartesian product of multiple arrays in JavaScript
(35 answers)
How to utilise TypeScript Variadic Tuple Types for a Cartesian Product function?
(2 answers)
Closed 10 months ago.
Hi can anyone please help me. I want to be able to generate all possible combinations of the array but only certain elements will be changing given two possible numbers to choose from. Their position is important too.
For example: let arr = [1,2,3, (4 or 5), (6 or 7), (8 or 9)]
I want to generate
1,2,3,4,6,8
1,2,3,4,6,9
1,2,3,4,7,8
.
.
.
.
This goes on until 1,2,3,5,7,9
I know it is very similar to binary numbers, I just don't know how to go about it and also the array may change in size so it can lead to a lot of options and possible outcomes.
Please. Someone help.
Edit: clarified the combinations I want to generate

How many conditions are allowed in an "if" statement in JavaScript? [duplicate]

This question already has answers here:
Maximum conditions inside if in javascript
(3 answers)
Closed 2 years ago.
Is there a limit to how many conditions (e.g., logical OR conditions) that JavaScript allows me to have in an if statement, or can I have as many as I want?
If technically have not limits, but If you want a clean code, I recommend you use switch case.
Know more about how to write better conditionals statements here: https://www.digitalocean.com/community/posts/5-tips-to-write-better-conditionals-in-javascript
You can have as many as you want 🙂
There is no limit to use as many as conditions you want , but if we have so many conditions its always good practice to use SWITCH case to make code clean and efficent.

Split string by arithmetic operators [duplicate]

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 3 years ago.
I have a string "str1+str2-str3*str4".
I want to split it so I get an array
['str1','+','str2','-','str3','*','str4'].
Could someone help provide a solution?
If JS split lets capture groups become elements, then this should work
/([-+*\/])/
if not, I suggest using a regular find all type thing
using this
/([-+*\/]|[^-+*\/]+)/
otherwise, I'll just delete this.

Does .length in javascript iterate through the whole array? [duplicate]

This question already has answers here:
Is reading the `length` property of an array really that expensive an operation in JavaScript?
(6 answers)
Closed 4 years ago.
I'm an front end webdev intern at a small company where I'm making a panel which displays database statistics.
Now I noticed that on my panel which gives the count for how many entries are in a specific array the specific stat always takes a while to load (my other statistics appear nearly instantly).
Now my question is, does array.length actually loop though the whole array to get it's length?
The length is about 17000 and takes about 5 seconds to appear so I'm guessing that's the time it takes to loop through such a big array.
It is implementation dependent. The optimal implementation should know its length, where as the lazy one would iterate to figure this out.

How can I write javascript like this ruby code (array matching)? [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 9 years ago.
This is my ruby code.
array1= [1,2,3,4,5]
array2= [2,3,4,5,6]
if (array1 & array2).size == 4
puts "4 match"
How can I write the same thing in javascript? Is there any material like ruby-doc in javascript?
There isn't a native method do do it.
Have a look at here : Simplest code for array intersection in javascript
The best documentation I know about javascript and html5 is the Mozilla developer network.

Categories