Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content model: Avoid classes.contains, which does extra work like creating a Set #497

Closed
chrisbobbe opened this issue Jan 30, 2024 · 0 comments · Fixed by #505
Closed

content model: Avoid classes.contains, which does extra work like creating a Set #497

chrisbobbe opened this issue Jan 30, 2024 · 0 comments · Fixed by #505
Labels
a-content Parsing and rendering Zulip HTML content, notably message contents performance Smooth and responsive UI; fixing jank, stutters, and lag

Comments

@chrisbobbe
Copy link
Collaborator

Greg and I discovered on our call today that classes.contains does some extra work:

  /// Determine if this element contains the class [value].
  ///
  /// This is the Dart equivalent of jQuery's
  /// [hasClass](http://api.jquery.com/hasClass/).
  @override
  bool contains(Object? value) => readClasses().contains(value);
  @override
  Set<String> readClasses() {
    final s = LinkedHashSet<String>();
    final classname = _element.className;

    for (var name in classname.split(' ')) {
      final trimmed = name.trim();
      if (trimmed.isNotEmpty) {
        s.add(trimmed);
      }
    }
    return s;
  }

When we're checking that there are no classes, we should just check if className is empty. When we're checking that there is one class, we should just do string equality on className. When we're checking that there is more than one class, we might want to do a regular expression or something.

@chrisbobbe chrisbobbe added the a-content Parsing and rendering Zulip HTML content, notably message contents label Jan 30, 2024
@gnprice gnprice added this to the Beta 3 milestone Feb 1, 2024
@gnprice gnprice added the performance Smooth and responsive UI; fixing jank, stutters, and lag label Feb 1, 2024
chrisbobbe added a commit to chrisbobbe/zulip-flutter that referenced this issue Feb 2, 2024
It turns out that `classes.contains` instantiates a Set every time
it's called. That's unfortunate.

So, remove some of this work by just checking the class string
(`className`) directly, in the case where we expect just one class.
When we expect two or more classes, we'll want a check that doesn't
enforce a particular order, so perhaps we'll use a regular
expression for those, or something, in later work. See zulip#497.

Fixes-partly: zulip#497
gnprice pushed a commit to gnprice/zulip-flutter that referenced this issue Feb 5, 2024
It turns out that `classes.contains` instantiates a Set every time
it's called. That's unfortunate.

So, remove some of this work by just checking the class string
(`className`) directly, in the case where we expect just one class.
When we expect two or more classes, we'll want a check that doesn't
enforce a particular order, so perhaps we'll use a regular
expression for those, or something, in later work. See zulip#497.

Fixes-partly: zulip#497
gnprice added a commit to gnprice/zulip-flutter that referenced this issue Feb 5, 2024
And add another test for more complete coverage.

This removes our last use of the `classes` class set,
so it completes zulip#497.

Fixes: zulip#497
chrisbobbe added a commit that referenced this issue Feb 6, 2024
It turns out that `classes.contains` instantiates a Set every time
it's called. That's unfortunate.

So, remove some of this work by just checking the class string
(`className`) directly, in the case where we expect just one class.
When we expect two or more classes, we'll want a check that doesn't
enforce a particular order, so perhaps we'll use a regular
expression for those, or something, in later work. See #497.

Fixes-partly: #497
chrisbobbe pushed a commit that referenced this issue Feb 6, 2024
And add another test for more complete coverage.

This removes our last use of the `classes` class set,
so it completes #497.

Fixes: #497
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-content Parsing and rendering Zulip HTML content, notably message contents performance Smooth and responsive UI; fixing jank, stutters, and lag
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants