jquery.accessKeyLabel: Avoid needless mw.Message#parser in updateTooltip()
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 27 Jun 2016 20:37:47 +0000 (21:37 +0100)
committerKrinkle <krinklemail@gmail.com>
Tue, 28 Jun 2016 22:13:14 +0000 (22:13 +0000)
When calling mw.util.addPortletLink(), Chrome on a MacBook Pro spends 3ms
in mw.Message#parser for each link that doesn't even have a title.

> addPortletLink: updateTooltipAccessKeys: updateTooltip: msg: mw.Message.parser

Change-Id: Id6c26e31eb2c1431fe9725e608813bdf40069207

resources/src/jquery/jquery.accessKeyLabel.js

index 519e39b..6ff2e01 100644 (file)
@@ -113,18 +113,19 @@ function getAccessKeyLabel( element ) {
  * @param {HTMLElement} titleElement Element with the title to update (may be the same as `element`)
  */
 function updateTooltipOnElement( element, titleElement ) {
-       var array = ( mw.msg( 'word-separator' ) + mw.msg( 'brackets' ) ).split( '$1' ),
-               regexp = new RegExp( $.map( array, mw.RegExp.escape ).join( '.*?' ) + '$' ),
-               oldTitle = titleElement.title,
-               rawTitle = oldTitle.replace( regexp, '' ),
-               newTitle = rawTitle,
-               accessKeyLabel = getAccessKeyLabel( element );
-
-       // don't add a title if the element didn't have one before
+       var oldTitle, parts, regexp, newTitle, accessKeyLabel;
+
+       oldTitle = titleElement.title;
        if ( !oldTitle ) {
+               // don't add a title if the element didn't have one before
                return;
        }
 
+       parts = ( mw.msg( 'word-separator' ) + mw.msg( 'brackets' ) ).split( '$1' );
+       regexp = new RegExp( $.map( parts, mw.RegExp.escape ).join( '.*?' ) + '$' );
+       newTitle = oldTitle.replace( regexp, '' );
+       accessKeyLabel = getAccessKeyLabel( element );
+
        if ( accessKeyLabel ) {
                // Should be build the same as in Linker::titleAttrib
                newTitle += mw.msg( 'word-separator' ) + mw.msg( 'brackets', accessKeyLabel );