Skip to content

Commit

Permalink
perf(createlookup): skip lookup for just one item
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jan 25, 2020
1 parent d50ea52 commit f8d089d
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 61 deletions.
20 changes: 8 additions & 12 deletions benchmark/cases/case_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,14 @@ module.exports = {
],
disabled && classes.disabled,
fullWidth && classes.fullWidth,
{
outlined: [
classes.outlined,
{
primary: classes.outlinedPrimary,
secondary: classes.outlinedSecondary,
}[color],
],
}[variant],
{
inherit: classes.colorInherit,
}[color],
variant === 'outlined' && [
classes.outlined,
{
primary: classes.outlinedPrimary,
secondary: classes.outlinedSecondary,
}[color],
],
color === 'inherit' && classes.colorInherit,
);
},
};
20 changes: 5 additions & 15 deletions src/visitors/createObjectKeyLookups.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createLogicalAndExpression,
isNestedLogicalAndExpression,
} from '../utils/helpers';
import generate from '@babel/generator';

function matchLeftOrRight(node, check) {
return check(node.left) || check(node.right);
Expand Down Expand Up @@ -60,6 +59,10 @@ function combineFromArray(arr) {

// Create the objects
_.map(group => {
if (group.length === 1) {
return createLogicalAndExpression(group[0]);
}

const properties = group.reduce((acc, row) => {
let key = row[0].right;
// If possible, use a identifier as the key, saves 2 characters
Expand All @@ -71,20 +74,7 @@ function combineFromArray(arr) {
return acc;
}, []);

const lookupExpression = t.memberExpression(
t.objectExpression(properties),
group[0][0].left,
true,
);
if (group.length > 1) {
return lookupExpression;
}

// If the size is the same, use the original
const original = createLogicalAndExpression(group[0]);
const a = generate(original, { compact: true }).code;
const b = generate(lookupExpression, { compact: true }).code;
return a.length <= b.length ? original : lookupExpression;
return t.memberExpression(t.objectExpression(properties), group[0][0].left, true);
}),
)(match);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
clsx({
[foo.a]: bar === 'up',
[foo.b]: bar === 'down',
[foo.b]: bar === 'down' && !inProp && collapsedHeight === '0px',
[foo.c]: baz === 'left',
[foo.d]: baz === 'right',
});
4 changes: 0 additions & 4 deletions test/fixtures/create-lookup/multiple-checks/code.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/create-lookup/multiple-checks/output.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixtures/create-lookup/multiple-matches/code.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/fixtures/create-lookup/multiple-matches/output.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
clsx(
{
up: foo.a,
down: foo.b,
down: !inProp && collapsedHeight === '0px' && foo.b,
}[bar],
{
left: foo.c,
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/create-lookup/same-length/code.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/create-lookup/same-length/output.js

This file was deleted.

4 changes: 1 addition & 3 deletions test/fixtures/strip-literals/boolean-literal/output.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
clsx(
classes.cellHide,
!print && 'datatables-noprint',
{
stacked: classes.cellStacked,
}[options.responsive],
options.responsive === 'stacked' && classes.cellStacked,
);
4 changes: 1 addition & 3 deletions test/fixtures/strip-literals/string-literal/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ clsx(
'foo',
classes.root,
!print && 'datatables-noprint',
{
stacked: classes.cellStacked,
}[options.responsive],
options.responsive === 'stacked' && classes.cellStacked,
);

0 comments on commit f8d089d

Please sign in to comment.