Repository navigation
perf: cache native styles that use data-[…] variants #764
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
Merged
Brentlok
merged 5 commits into
uni-stack:main
from
JaydeepAgravat:perf/cache-data-attribute-styles
Nov 6, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0ac7441
perf: cache native styles that use data-[…] variants
JaydeepAgravat 53011ba
perf: share one cache entry for data values no rule expects
JaydeepAgravat 83d1c8e
perf: keep the first resolve of class strings as cheap as before
JaydeepAgravat 54a9cc0
refactor: key data variant styles by raw prop values
JaydeepAgravat f1efd5f
refactor: fill the data attribute names map in resolveStyles
JaydeepAgravat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/uniwind/tests/native/core/data-attributes-cache.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { UniwindStore } from '../../../src/core/native/store' | ||
| import { TW_BLUE_500, TW_RED_500 } from '../../consts' | ||
|
|
||
| const context = { scopedTheme: null, rtl: null, variables: null } | ||
| const plain = 'bg-blue-500 opacity-50' | ||
| const withData = 'bg-blue-500 data-[active=true]:bg-red-500' | ||
| const withNumberLikeValue = 'bg-blue-500 data-[x=1]:bg-red-500' | ||
|
|
||
| const countResolves = (run: () => void) => { | ||
| const spy = jest.spyOn(UniwindStore as any, 'resolveStyles') | ||
|
|
||
| run() | ||
|
|
||
| const calls = spy.mock.calls.length | ||
|
|
||
| spy.mockRestore() | ||
|
|
||
| return calls | ||
| } | ||
|
|
||
| const getBackground = (className: string, props?: Record<string, any>) => | ||
| UniwindStore.getStyles(className, props, undefined, context).styles.backgroundColor | ||
|
|
||
| describe('Caching styles that use data attributes', () => { | ||
| test('resolves a class string with a data variant only once for the same props', () => { | ||
| const calls = countResolves(() => { | ||
| for (let i = 0; i < 1000; i++) { | ||
| getBackground(withData, { 'data-active': true }) | ||
| } | ||
| }) | ||
|
|
||
| expect(calls).toBe(1) | ||
| }) | ||
|
|
||
| test('returns the right style when a data attribute changes, and caches each value', () => { | ||
| const calls = countResolves(() => { | ||
| expect(getBackground(withData, { 'data-active': true })).toBe(TW_RED_500) | ||
| expect(getBackground(withData, { 'data-active': false })).toBe(TW_BLUE_500) | ||
| expect(getBackground(withData, undefined)).toBe(TW_BLUE_500) | ||
| expect(getBackground(withData, { 'data-active': 'true' })).toBe(TW_RED_500) | ||
| expect(getBackground(withData, { 'data-active': true })).toBe(TW_RED_500) | ||
| expect(getBackground(withData, { 'data-active': false })).toBe(TW_BLUE_500) | ||
| }) | ||
|
|
||
| // One resolve each for true, false, missing and "true"; the repeats hit the cache | ||
| expect(calls).toBe(4) | ||
| }) | ||
|
|
||
| test('keeps number and string values apart', () => { | ||
| expect(getBackground(withNumberLikeValue, { 'data-x': '1' })).toBe(TW_RED_500) | ||
| expect(getBackground(withNumberLikeValue, { 'data-x': 1 })).toBe(TW_BLUE_500) | ||
| expect(getBackground(withNumberLikeValue, { 'data-x': '1' })).toBe(TW_RED_500) | ||
| }) | ||
|
|
||
| test('ignores data props that the class string does not use', () => { | ||
| const calls = countResolves(() => { | ||
| getBackground(withData, { 'data-active': true, 'data-testid': 'a' }) | ||
| getBackground(withData, { 'data-active': true, 'data-testid': 'b' }) | ||
| getBackground(plain, { 'data-testid': 'a' }) | ||
| getBackground(plain, { 'data-testid': 'b' }) | ||
| }) | ||
|
|
||
| expect(calls).toBe(2) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.