mirror of
https://github.com/lovell/sharp.git
synced 2025-12-06 12:01:41 +01:00
Uses the recommended rules apart from complexity/useArrowFunction, which would affect about 1700 lines of code with little benefit right now. This is something that can be addressed over time.
76 lines
1.8 KiB
JavaScript
76 lines
1.8 KiB
JavaScript
// Copyright 2013 Lovell Fuller and others.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
const imagemagick = require('imagemagick');
|
|
const gm = require('gm');
|
|
const assert = require('node:assert');
|
|
const Benchmark = require('benchmark');
|
|
|
|
const sharp = require('../../');
|
|
const fixtures = require('../fixtures');
|
|
|
|
sharp.cache(false);
|
|
|
|
const min = 320;
|
|
const max = 960;
|
|
|
|
const randomDimension = function () {
|
|
return Math.ceil((Math.random() * (max - min)) + min);
|
|
};
|
|
|
|
new Benchmark.Suite('random').add('imagemagick', {
|
|
defer: true,
|
|
fn: function (deferred) {
|
|
imagemagick.resize({
|
|
srcPath: fixtures.inputJpg,
|
|
dstPath: fixtures.path('output.jpg'),
|
|
quality: 0.8,
|
|
width: randomDimension(),
|
|
height: randomDimension(),
|
|
format: 'jpg',
|
|
filter: 'Lanczos'
|
|
}, function (err) {
|
|
if (err) {
|
|
throw err;
|
|
} else {
|
|
deferred.resolve();
|
|
}
|
|
});
|
|
}
|
|
}).add('gm', {
|
|
defer: true,
|
|
fn: function (deferred) {
|
|
gm(fixtures.inputJpg)
|
|
.resize(randomDimension(), randomDimension())
|
|
.filter('Lanczos')
|
|
.quality(80)
|
|
.toBuffer(function (err, buffer) {
|
|
if (err) {
|
|
throw err;
|
|
} else {
|
|
assert.notStrictEqual(null, buffer);
|
|
deferred.resolve();
|
|
}
|
|
});
|
|
}
|
|
}).add('sharp', {
|
|
defer: true,
|
|
fn: function (deferred) {
|
|
sharp(fixtures.inputJpg)
|
|
.resize(randomDimension(), randomDimension())
|
|
.toBuffer(function (err, buffer) {
|
|
if (err) {
|
|
throw err;
|
|
} else {
|
|
assert.notStrictEqual(null, buffer);
|
|
deferred.resolve();
|
|
}
|
|
});
|
|
}
|
|
}).on('cycle', function (event) {
|
|
console.log(String(event.target));
|
|
}).on('complete', function () {
|
|
const winner = this.filter('fastest').map('name');
|
|
assert.strictEqual('sharp', String(winner), `sharp was slower than ${winner}`);
|
|
}).run();
|