Skip to content

Commit

Permalink
perf(core): Optimize data.kindOf data.isOfKind (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blagoja Evkoski authored and ognen committed Dec 8, 2018
1 parent 41211c8 commit 86ee5d1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
69 changes: 69 additions & 0 deletions packages/core/src/data/__tests__/element-benchmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @jest-environment node
*/

import { Suite } from 'benchmark'
import I from 'immutable'

import { canonical, isOfKind, kindOf } from '../'

describe('element benchmarks', () => {
const detective1 = I.fromJS({
kind: 'detective',
name: 'Sherlock Holmes',
})
const detective2 = I.fromJS({
kind: ['detective', 'the'],
name: 'Sherlock Holmes',
})
const detective3 = I.fromJS({
kind: ['detective', 'the', 'smartest'],
name: 'Sherlock Holmes',
})
const detective4 = I.fromJS({
kind: ['detective', 'the', 'smartest', 'ever'],
name: 'Sherlock Holmes',
})

test.skip('canonical', () => {
new Suite()
.add('canonical memoized', () => {
canonical('detective')
canonical(['detective', 'the'])
canonical(['detective', 'the', 'smartest'])
canonical(['detective', 'the', 'smartest', 'ever'])
})
.on('cycle', function(event) {
console.log(event.target.toString())
})
.run()
})

test.skip('isOfKind', () => {
new Suite()
.add('isOfKind memoized', () => {
isOfKind('detective', detective3)
isOfKind(['detective', 'the'], detective3)
isOfKind(['detective', 'the', 'smartest'], detective3)
isOfKind(['detective', 'the', 'smartest', 'ever'], detective3)
})
.on('cycle', function(event) {
console.log(event.target.toString())
})
.run()
})

test.skip('kindOf', () => {
new Suite()
.add('kindOf memoized', () => {
kindOf(detective1)
kindOf(detective2)
kindOf(detective3)
kindOf(detective4)
})
.on('cycle', function(event) {
console.log(event.target.toString())
})
.run()
})
})
4 changes: 2 additions & 2 deletions packages/core/src/data/__tests__/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ describe('element', function() {
})

it('properly normalizes element kinds', function() {
expect(canonical(null)).toEqual(null)
expect(canonical(true)).toEqual(null)
expect(canonical(null) == null).toBe(true)
expect(canonical(true) == null).toBe(true)
expect(canonical('component')).toEqual(List.of('component'))
expect(canonical(['component', 'test'])).toEqual(
List.of('component', 'test')
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/data/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import * as R from 'ramda'
import invariant from 'invariant'
import deprecated from '../log/deprecated'
import { List, Seq, is, Iterable } from 'immutable'
import memoize from '../registry/memoize'
import deprecated from '../log/deprecated'

/**
* Checks if a given object is of the provided kind.
Expand Down Expand Up @@ -48,7 +49,7 @@ export function isElementRef(obj) {
* @returns {*}
*/
export const isExactlyOfKind = R.curry(function isExactlyOfKind(kind, element) {
if (element == null || kindOf(element) == null) {
if (element == null) {
return false
}

Expand Down Expand Up @@ -114,17 +115,9 @@ export function ancestorKinds(ref) {
return Seq(subKinds())
}

/**
* @param ref an element reference
* @returns the canonical version for the reference kind
*/
export function canonical(ref) {
return normalize(ref)
}

function normalize(kind) {
function _normalize(kind) {
if (typeof kind === 'string') {
return normalize([kind])
return List.of(kind)
}

if (Array.isArray(kind)) {
Expand All @@ -142,6 +135,13 @@ function normalize(kind) {
return null
}

const normalize = memoize(_normalize)

/**
* @returns the canonical version for the reference kind
*/
export const canonical = normalize

export function pathsToChildElements(element) {
return childPositions(element).flatMap(childrenPath => {
const children = element.get(childrenPath)
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/registry/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { List } from 'immutable'
import Trie from './impl/Trie'

export function adaptKey(key) {
// prettier-ignore
if (key instanceof List) {
// cast
if (key.size === 0) return []
if (key.size === 1) return [key.get(0)]
if (key.size === 2) return [key.get(0), key.get(1)]
if (key.size === 3) return [key.get(0), key.get(1), key.get(2)]
if (key.size === 4) return [key.get(0), key.get(1), key.get(2), key.get(3)]
if (key.size === 5) return [key.get(0), key.get(1), key.get(2), key.get(3), key.get(4)]
return key.toArray()
}
return key
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/registry/__tests__/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@

import { List } from 'immutable'

import Registry from '../Registry'
import Registry, { adaptKey } from '../Registry'

describe('adaptKey', () => {
it('properly adapts keys', () => {
expect(adaptKey(null)).toEqual(null)
expect(adaptKey([])).toEqual([])
expect(adaptKey(List([]))).toEqual([])
expect(adaptKey(List.of('one', 'two', 'three'))).toEqual([
'one',
'two',
'three',
])
expect(
adaptKey(List.of('one', 'two', 'three', 'four', 'five', 'six', 'seven'))
).toEqual(['one', 'two', 'three', 'four', 'five', 'six', 'seven'])
})
})

describe('Registry', () => {
const registry = new Registry()
Expand Down

0 comments on commit 86ee5d1

Please sign in to comment.