Skip to content

Commit

Permalink
Add benchmarks for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkasm committed Dec 26, 2016
1 parent e274907 commit be0ab83
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
40 changes: 40 additions & 0 deletions bench/benchSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import Stylesheet from '../src/stylesheet'
import Node from '../src/node'
import { parseSelector } from '../src/selectors'

const s = new Stylesheet()

const appNode = new Node('App', {}, undefined, s)
const appViewNode = new Node('AppView', {}, appNode, s)
const elemNode = new Node('Elem', {variant: ['selected']}, appViewNode, s)
const itemNode = new Node('Item', {}, elemNode, s)
const textNode = new Node('Text', {prop1: 1}, itemNode, s)

function createBenchFunc (sel, node, expected) {
return function () {
const actual = sel.matchContext(node)
if (actual !== expected) {
throw new Error(`expected ${expected} but got ${actual}`)
}
}
}

export default [
{
name: 'selectors: simple match',
fn: createBenchFunc(parseSelector('Text'), textNode, true)
},
{
name: 'selectors: simple not match',
fn: createBenchFunc(parseSelector('NotExists'), textNode, false)
},
{
name: 'selectors: complex match',
fn: createBenchFunc(parseSelector('AppView Elem.selected Text'), textNode, true)
},
{
name: 'selectors: complex not match',
fn: createBenchFunc(parseSelector('NotExists Elem Text'), textNode, false)
}
]
5 changes: 5 additions & 0 deletions bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
import Benchmark from 'benchmark'

import benchStylesheet from './benchStylesheet'
import selectorsBenchmarks from './benchSelectors'

const suite = new Benchmark.Suite()

for (let i = 0; i < selectorsBenchmarks.length; ++i) {
suite.add(selectorsBenchmarks[i])
}

suite.add('Stylesheet#getProps', benchStylesheet)

suite.on('cycle', function (e) {
Expand Down

0 comments on commit be0ab83

Please sign in to comment.