From e7f82b74182acf425b12f2defbe543c335468ff9 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Thu, 24 Oct 2019 12:43:55 +0200 Subject: [PATCH 1/4] Add benchmmark Still has +-20% in median runtime --- src/__tests__/benchmark/byRole.js | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/__tests__/benchmark/byRole.js diff --git a/src/__tests__/benchmark/byRole.js b/src/__tests__/benchmark/byRole.js new file mode 100644 index 00000000..50584207 --- /dev/null +++ b/src/__tests__/benchmark/byRole.js @@ -0,0 +1,46 @@ +import {cleanup, renderIntoDocument} from '../helpers/test-utils' + +test('byRole performance', () => { + const samples = 1000 + const times = Array(samples) + + for (let run = 0; run <= samples; run += 1) { + const {getAllByRole} = renderIntoDocument(` +
+
+

A list of things to do

+

+ A section describing how to do. And now a list with things +

+
    +
  • do this
  • +
  • do that
  • +
  • do it now
  • +
+
+

+
+
+`) + const start = performance.now() + getAllByRole('listitem') + times[run] = performance.now() - start + cleanup() + } + + const avg = times.reduce((sum, n) => sum + n, 0) / times.length + const median = times.sort((a, b) => a - b)[Math.floor(times.length / 2)] + const percentile95 = times.sort((a, b) => a - b)[ + Math.floor(times.length * 0.95) + ] + + expect(` +95th percentile: ${Math.round(percentile95 * 1000)}µs +median: ${Math.round(median * 1000)}µs +avg: ${Math.round(avg * 1000)}µs`).toMatchInlineSnapshot(` + " + 95th percentile: 6045µs + median: 4680µs + avg: 4775µs" + `) +}) From 29a869bd770e624389166e81f1737ee3ea3a13c0 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Thu, 24 Oct 2019 17:27:24 +0200 Subject: [PATCH 2/4] Extract isSubtreeInaccessible Useful to inject more performant implementations --- src/__tests__/role-helpers.js | 2 +- src/__tests__/role.js | 12 ++++--- src/role-helpers.js | 59 ++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/__tests__/role-helpers.js b/src/__tests__/role-helpers.js index 728e0d00..9563b66c 100644 --- a/src/__tests__/role-helpers.js +++ b/src/__tests__/role-helpers.js @@ -171,7 +171,7 @@ test.each([ ['
', false], ['