Skip to content

Commit

Permalink
perf: ⚡️ create units object only once
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jul 11, 2018
1 parent 2bf0670 commit 4c39f31
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ __Motto of `nano-css` is simple__: *create the smallest possible CSS-in-JS libra
- [`extract`](./docs/extract.md)
- [`sourcemaps`](./docs/sourcemaps.md)
- [`safe`](./docs/safe.md)
- [`units`](./docs/units.md)
- [Presets](./docs/Presets.md)
- [Server-side rendering](./docs/SSR.md)

Expand Down
5 changes: 5 additions & 0 deletions addon/__tests__/units.dev.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

process.env.NODE_ENV = 'development';

require('./units.test');
6 changes: 6 additions & 0 deletions addon/__tests__/units.server.dev.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @jest-environment node */
'use strict';

process.env.NODE_ENV = 'development';

require('./units.test');
4 changes: 4 additions & 0 deletions addon/__tests__/units.server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @jest-environment node */
'use strict';

require('./units.test');
64 changes: 32 additions & 32 deletions addon/units.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
'use strict';

exports.addon = function (renderer) {
var units = {};
var list = [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'em',
'ex',
'ch',
'rem',
'vw',
'vh',
'deg',
'vmin',
'vmax',
];
var units = {};
var list = [
'px',
'cm',
'mm',
'in',
'pt',
'pc',
'em',
'ex',
'ch',
'rem',
'vw',
'vh',
'deg',
'vmin',
'vmax',
];

function f (unit, val) {
return val + unit;
}
function f (unit, val) {
return val + unit;
}

for (var i = 0; i < list.length; i++) {
var unit = list[i];
for (var i = 0; i < list.length; i++) {
var unit = list[i];

units[unit] = f.bind(null, unit);
}
units[unit] = f.bind(null, unit);
}

units.inch = function (val) {
return val + 'in';
};
units.inch = function (val) {
return val + 'in';
};

units.pct = function (val) {
return val + '%';
};
units.pct = function (val) {
return val + '%';
};

exports.addon = function (renderer) {
renderer.assign(renderer, units);
renderer.units = units;
};

0 comments on commit 4c39f31

Please sign in to comment.