Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Improve jqLiteRemoveClass performance #16078

Closed
@aresn

Description

@aresn

I would like to propose a slight modification to jqLiteRemoveClass function to increase it's performance.

Current implementation constantly reads the "class" value from DOM and writes it back to the DOM inside a for loop.

Here is what I have created for our local branch of angular:

function jqLiteRemoveClass(element, cssClasses) {
  if (cssClasses && element.setAttribute) {
    var currentClass = element.getAttribute('class');

    if (currentClass) {
      var cssClass;
      var curValue;
      curValue = currentClass = trim(currentClass);
      var classNames = cssClasses.split(' ');

      for (var i = 0, len = classNames.length; i < len; i++) {
        cssClass = trim(classNames[i]);
        currentClass = trim((' ' + currentClass + ' ')
          .replace(/[\n\t]/g, ' ')
          .replace(' ' + cssClass + ' ', ' '));
      }
      if (currentClass !== curValue) {
        element.setAttribute('class',currentClass);
      }
    }
  }
}

Here is a link to a jsPerf that I have created that compares the two functions :
https://jsperf.com/compare-angular-remove-class-with-improved-version/1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions