Skip to content

Commit

Permalink
chore: refactor compare.js (should yield same results)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Nov 19, 2024
1 parent f033fb3 commit 3d8bdc5
Showing 1 changed file with 88 additions and 172 deletions.
260 changes: 88 additions & 172 deletions test/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import parse from 'color-parse';
import parser from 'color-parser';
import kcolor from '../dist/color.esm.js';

const {get: ostring} = colorString;
const { get: ostring } = colorString;

Check failure on line 11 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

There should be no space after '{'

Check failure on line 11 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

There should be no space before '}'

Check failure on line 11 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

There should be no space after '{'

Check failure on line 11 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

There should be no space before '}'

const strings = [
const parsedStrings = [
'#d6F',
'#AABB',
'#555555',
Expand All @@ -19,7 +19,7 @@ const strings = [
'rgba(255, 0, 0, 0.5)',
'hsla(240, 100, 60, 0.5)',
'blue'
];
]

Check failure on line 22 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 22 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

const parsers = {
'@kurkle/color': kcolor,
Expand All @@ -29,179 +29,95 @@ const parsers = {
'color-parse': parse,
'color-parser': parser,
'color-string': ostring
};
const objects = {
'@kurkle/color': true,
'chartjs-color': true
};
}

Check failure on line 32 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 32 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

const parserNames = Object.keys(parsers);

const manipulators = {
'@kurkle/color': kcolor,
'chroma-js': chroma,
'chartjs-color': color
};
const manipulatorNames = Object.keys(manipulators);
const called = ['tinycolor2', 'chroma-js', 'color-parse', 'color-parser', 'color-string']

Check failure on line 34 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 34 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon
const constructed = ['@kurkle/color', 'chartjs-color']

Check failure on line 35 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 35 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon
const manipulators = ['@kurkle/color', 'chroma-js', 'chartjs-color']

Check failure on line 36 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 36 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

const options = {
initCount: 1,
maxTime: 4
};
}

Check failure on line 41 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 41 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

const cycle = (event) => !event.target.error && console.log(`${event.target}`)

Check failure on line 43 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 43 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

const benchmarkSuites = []

Check failure on line 45 in test/compare.js

View workflow job for this annotation

GitHub Actions / Performance comparison

Missing semicolon

Check failure on line 45 in test/compare.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

// parsing suites

const cycle = function(event) {
if (!event.target.error) {
console.log(String(event.target));
for (const str of parsedStrings) {
const suite = new benchmark.Suite()

for (const lib of constructed) {
suite.add(`parse ${str}|${lib}`, () => {
const c = new parsers[lib](str)
if (!c) {
throw new Error('failed');
}
}, options)
}

for (const lib of called) {
suite.add(`parse ${str}|${lib}`, () => {
const c = parsers[lib](str)
if (!c) {
throw new Error('failed');
}
}, options)
}
};

strings.forEach(function(str) {
var _suite = new benchmark.Suite();
parserNames.forEach(function(lib) {
if (objects[lib]) {
_suite.add('parse ' + str + '|' + lib, function() {
var c = new parsers[lib](str);
if (!c) {
throw new Error('failed');
}
}, options);
} else {
_suite.add('parse ' + str + '|' + lib, function() {
var c = parsers[lib](str);
if (!c) {
throw new Error('failed');
}
}, options);
}
});
_suite
.on('cycle', cycle)
.run();
});

var suites = [];
var suite;

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('#aaaaaa');
suite.add('alpha|' + lib, function() {
c1.alpha(0.5);
}, options);
});
suites.push(['alpha', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('rgb(0, 100, 255)');
suite.add('negate|' + lib, function() {
c1.negate();
}, options);
});
suites.push(['negate', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('#aaaaaa');
suite.add('lighten|' + lib, function() {
c1.lighten(0.1);
}, options);
});
suites.push(['lighten', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('#aaaaaa');
suite.add('darken|' + lib, function() {
c1.darken(0.1);
}, options);
});
suites.push(['darken', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('saturate|' + lib, function() {
c1.saturate(0.5);
}, options);
});
suites.push(['saturate', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('desaturate|' + lib, function() {
c1.desaturate(0.5);
}, options);
});
suites.push(['desaturate', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('clearer|' + lib, function() {
c1.clearer(0.5);
}, options);
});
suites.push(['clearer', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('opaquer|' + lib, function() {
c1.opaquer(0.5);
}, options);
});
suites.push(['opaquer', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('#aaaaaa');
var c2 = new manipulators[lib]('#33333380');
suite.add('mix|' + lib, function() {
c1.mix(c2, 0.5);
}, options);
});
suites.push(['mix', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('clone|' + lib, function() {
c1.clone();
}, options);
});
suites.push(['clone', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('hexString|' + lib, function() {
c1.hexString();
}, options);
});
suites.push(['hexString', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('hslString|' + lib, function() {
c1.hslString();
}, options);
});
suites.push(['hslString', suite]);

suite = new benchmark.Suite();
manipulatorNames.forEach(function(lib) {
var c1 = new manipulators[lib]('hsl(100, 50%, 50%)');
suite.add('rgbString|' + lib, function() {
c1.rgbString();
}, options);
});
suites.push(['rgbString', suite]);

suites.forEach(function(arr) {
arr[1]
.on('cycle', cycle)
.run();
});

benchmarkSuites.push(suite)
}

// manipulation suites

const manipulationParams = {
alpha: [0.5],
negate: [],
lighten: [0, 1],
darken: [0, 1],
saturate: [0.5],
desaturate: [0.5],
clearer: [0.5],
opaquer: [0.5],
mix: ['#33333380', 0.5],
clone: [],
hexString: [],
hslString: [],
rgbString: [],
}

const manupulatedColors = {
alpha: '#aaaaaa',
negate: 'rgb(0, 100, 255)',
lighten: '#aaaaaa',
darken: '#aaaaaa',
mix: '#aaaaaa',
}

const parserFn = (lib, color) => constructed.includes(lib) ? new parsers[lib](color) : parsers[lib](color)

for (const fn of Object.keys(manipulationParams)) {
const suite = new benchmark.Suite();
const args = manipulationParams[fn];
const color = manupulatedColors[fn] ?? 'hsl(100, 50%, 50%)';

for (const lib of manipulators) {
const instance = parserFn(lib, color)

if (!(fn in instance)) continue // not supported

const instanceArgs = args.map((arg) => typeof arg === 'string' ? parserFn(lib, arg) : arg)

suite.add(`${fn}|${lib}`, () => {
instance[fn](...instanceArgs);
}, options);
}
benchmarkSuites.push(suite);
}

// run the suites
for (const suite of benchmarkSuites) {
suite.on('cycle', cycle).run()
}

0 comments on commit 3d8bdc5

Please sign in to comment.