Esanders has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/342015 )

Change subject: FindAndReplace: Use Intl.Collator instead of localeCompare
......................................................................

FindAndReplace: Use Intl.Collator instead of localeCompare

This is the recommended method for performing a large number
of comparisons and is about 100 times faster. Makes diacritic
search on long documents take 40ms instead of 4000ms.

Change-Id: I3ac6e92be7895bd0c7a336e7e9ae6bd11340083f
---
M src/dm/ve.dm.Document.js
M src/ui/dialogs/ve.ui.FindAndReplaceDialog.js
M src/ve.utils.js
3 files changed, 8 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/15/342015/1

diff --git a/src/dm/ve.dm.Document.js b/src/dm/ve.dm.Document.js
index 7ccffa6..5575365 100644
--- a/src/dm/ve.dm.Document.js
+++ b/src/dm/ve.dm.Document.js
@@ -1492,7 +1492,7 @@
  * @return {ve.Range[]} List of ranges where the string was found
  */
 ve.dm.Document.prototype.findText = function ( query, options ) {
-       var i, j, l, qLen, match, offset, lines, dataString, sensitivity,
+       var i, j, l, qLen, match, offset, lines, dataString, sensitivity, 
compare,
                ranges = [],
                text = this.data.getText(
                        true,
@@ -1529,13 +1529,14 @@
                }
        } else {
                qLen = query.length;
-               if ( options.diacriticInsensitiveString && 
ve.supportsLocaleCompareOptions ) {
+               if ( options.diacriticInsensitiveString && ve.supportsIntl ) {
                        sensitivity = options.caseSensitiveString ? 'case' : 
'base';
+                       compare = new Intl.Collator( this.lang, { sensitivity: 
sensitivity } ).compare;
                        // Iterate up to (and including) offset textLength - 
queryLength. Beyond that point
                        // there is not enough room for the query to exist
                        for ( offset = 0, l = text.length - qLen; offset <= l; 
offset++ ) {
                                j = 0;
-                               while ( text[ offset + j ].localeCompare( 
query[ j ], this.lang, { sensitivity: sensitivity } ) === 0 ) {
+                               while ( compare( text[ offset + j ], query[ j ] 
) === 0 ) {
                                        j++;
                                        if ( j === qLen ) {
                                                ranges.push( new ve.Range( 
offset, offset + qLen ) );
diff --git a/src/ui/dialogs/ve.ui.FindAndReplaceDialog.js 
b/src/ui/dialogs/ve.ui.FindAndReplaceDialog.js
index 66c84cb..0b547d9 100644
--- a/src/ui/dialogs/ve.ui.FindAndReplaceDialog.js
+++ b/src/ui/dialogs/ve.ui.FindAndReplaceDialog.js
@@ -92,7 +92,7 @@
        } );
        this.diacriticToggle = new OO.ui.ToggleButtonWidget( {
                icon: 'searchDiacritics',
-               iconTitle: ve.supportsLocaleCompareOptions ?
+               iconTitle: ve.supportsIntl ?
                        ve.msg( 'visualeditor-find-and-replace-diacritic' ) :
                        ve.msg( 
'visualeditor-find-and-replace-diacritic-unavailable' ),
                value: ve.userConfig( 'visualeditor-findAndReplace-diacritic' ),
@@ -308,7 +308,7 @@
        this.clearRenderedResultsCache();
        this.renderFragments();
        this.highlightFocused( true );
-       this.diacriticToggle.setDisabled( !ve.supportsLocaleCompareOptions || 
this.regexToggle.getValue() );
+       this.diacriticToggle.setDisabled( !ve.supportsIntl || 
this.regexToggle.getValue() );
        ve.userConfig( {
                'visualeditor-findAndReplace-findText': 
this.findText.getValue(),
                'visualeditor-findAndReplace-matchCase': 
this.matchCaseToggle.getValue(),
diff --git a/src/ve.utils.js b/src/ve.utils.js
index 8461d47..00dc8a0 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -184,14 +184,14 @@
 ve.extendObject = $.extend;
 
 /**
- * Feature detect if the browser supports localeCompare options flags
+ * Feature detect if the browser supports the Internationalization API
  *
  * Should work in Chrome>=24, FF>=29 & IE>=11
  *
  * @private
  * @property {boolean}
  */
-ve.supportsLocaleCompareOptions = 'e'.localeCompare( 'É', 'en', { sensitivity: 
'base' } ) === 0;
+ve.supportsIntl = !!( window.Intl && typeof Intl.Collator === 'function' );
 
 /**
  * @private

-- 
To view, visit https://gerrit.wikimedia.org/r/342015
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3ac6e92be7895bd0c7a336e7e9ae6bd11340083f
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to