# Faster String Comparisons

**URL:** <https://es.discourse.group/t/faster-string-comparisons/2444>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 9, 2025, 7:45am UTC](https://es.discourse.group/t/faster-string-comparisons/2444 "2025-10-09T07:45:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeeshan.abid](https://avatars.discourse-cdn.com/v4/letter/z/bbe5ce/32.png) [@zeeshan.abid](https://es.discourse.group/u/zeeshan.abid)\
**Post date:** [October 9, 2025, 7:45am UTC](https://es.discourse.group/t/faster-string-comparisons/2444/1 "2025-10-09T07:45:25Z")

</div>

Hello, I noticed that `toLower() === toLower()` is the common practice for comparing strings ignoring case sensitivity. I wanted to know is it possible to add a function like

`String.compare(str1, str2, IgnoreLowerCaseOption);`

This should in theory run faster than doing `toLower() === toLower()`

I already tried using `String.prototype.localeCompare` and it is slower than doing `toLower() === toLower()`

I asked this to be implemented in V8 but they told me it needs to be approved as part of ECMA specifications. Which is why I am stumbling around here.

Thanks

> **[Optimise string comparisons ignoring case sensativity](https://groups.google.com/g/v8-dev/c/7ip7AowVGHY/m/G7SjYfWfAwAJ)**

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [October 9, 2025, 12:36pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/2 "2025-10-09T12:36:46Z")

</div>

I like the idea, but would prefer the case-ignoring comparison to use a different function.

1. It’s easier to use with `.sort()`.
2. It’s not immediately clear what a boolean even means in that context when you’re reading it.

Also, the existing precedent with locale comparisons prefers different functions (and also puts them on the prototype).

---

<div class="post-metadata">

**Author:** ![zeeshan.abid](https://avatars.discourse-cdn.com/v4/letter/z/bbe5ce/32.png) [@zeeshan.abid](https://es.discourse.group/u/zeeshan.abid)\
**Post date:** [October 9, 2025, 1:47pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/3 "2025-10-09T13:47:48Z")

</div>

Thanks for the feedback, I am not too attached to the interface of the function. The thing I would like is **not** to create new strings that are lowercase just to do a comparison between 2 strings.

Also, I was thinking `IgnoreLowerCaseOption` could be a more general object which has a field for ignoring case sensitivity. I am sure in the future there could be other options that may get added.

So perhaps something like  
`String.prototype.compare(str1, str2, { caseSensitivity: false });`  
or  
`str1.comapre(str2, { caseSensitivity: false });`

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [October 9, 2025, 1:55pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/4 "2025-10-09T13:55:04Z")

</div>

> [@zeeshan.abid](#):
>
> Also, I was thinking `IgnoreLowerCaseOption` could be a more general object which has a field for ignoring case sensitivity. I am sure in the future there could be other options that may get added.

Could you give some possible examples of this that wouldn't be better suited for `localeCompare` and `localeCompareIgnoreCase` (which already accept options objects, by the way)?

---

<div class="post-metadata">

**Author:** ![zeeshan.abid](https://avatars.discourse-cdn.com/v4/letter/z/bbe5ce/32.png) [@zeeshan.abid](https://es.discourse.group/u/zeeshan.abid)\
**Post date:** [October 9, 2025, 2:30pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/5 "2025-10-09T14:30:18Z")

</div>

I was suggesting it be an object for future proofing it but as I mentioned what I care about is just to optimize the string comparison. So something like `String.prototype.compareIgnoreCase(str1, str2)` is fine by me.

As mentioned this is not really the same thing as `localeCompare` because it goes through Intl.Collator API and that causes it to be a lot slower, Even if you do something like this

`const collator = ` **`new`** ` `_`Intl`_`.`_`Collator`_`(undefined, { sensitivity: 'accent' });`  
`collator.`_`compare`_`(str1, str2) === 0;`

Not including the creation (first statement) of collator.

`collator.compare(str1, str2)` is still slower than `toLower() === toLower()` which is not optimized because it is creating 2 new strings that are lowercase and then doing the comparison.

I would recommend looking at how other languages do this as a reference. As an example C# does this  
`string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase)`  
This is faster than lower casing and comparing the strings

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [October 9, 2025, 6:47pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/6 "2025-10-09T18:47:21Z")

</div>

We already have this:

```javascript
"hello".localeCompare("HELLO", undefined, { sensitivity: 'accent' }) // 0

```

The fact that it’s slower than using `toLower` just means engines haven’t put in the work to make it fast. Adding a different way of doing the same thing wouldn’t suddenly cause them to put in that work.

---

<div class="post-metadata">

**Author:** ![zeeshan.abid](https://avatars.discourse-cdn.com/v4/letter/z/bbe5ce/32.png) [@zeeshan.abid](https://es.discourse.group/u/zeeshan.abid)\
**Post date:** [October 9, 2025, 8:08pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/7 "2025-10-09T20:08:48Z")

</div>

From my understanding `toLower() === toLower()` is fundamentally different than `"hello".localeCompare("HELLO", undefined, { sensitivity: 'accent' })`

From someone smarter than me  
`String.prototype.localeCompare computes equivalence, which is much `  
`more complex and complicated than the case conversion that `  
`String.prototype.toLowerCase performs. For example, localeCompare `  
`considers "s\u0307\u0323" and "\u1E69" equivalent, whereas toLowerCase `  
`does not.`

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [October 9, 2025, 8:17pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/8 "2025-10-09T20:17:36Z")

</div>

…. Right, which is why you’d want to use `localeCompare` and not `toLowerCase`. In any context where you have inputs for which this matters, `localeCompare` is going to be more correct than `toLowerCase` + `===`. In a context where all inputs are ASCII engines could make `localeCompare` just as fast or faster, if they wanted to put in the effort.

---

<div class="post-metadata">

**Author:** ![zeeshan.abid](https://avatars.discourse-cdn.com/v4/letter/z/bbe5ce/32.png) [@zeeshan.abid](https://es.discourse.group/u/zeeshan.abid)\
**Post date:** [October 9, 2025, 8:39pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/9 "2025-10-09T20:39:44Z")

</div>

It will never be just as fast because there is a cost to checking if it is ASCII or Unicode. But I get what you mean. If you do go with that then it also creates an inconsistency with the toLowerCase(). Should toLowerCase() also handle cases like this?

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [October 9, 2025, 8:41pm UTC](https://es.discourse.group/t/faster-string-comparisons/2444/10 "2025-10-09T20:41:38Z")

</div>

Not automatically. That’s what `.normalize()` is for.
