Skip to content

Instantly share code, notes, and snippets.

@avaly
Created August 2, 2018 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avaly/159bda86789989d75fd3c0ec94bd55ec to your computer and use it in GitHub Desktop.
Save avaly/159bda86789989d75fd3c0ec94bd55ec to your computer and use it in GitHub Desktop.
sharp-blur-benchmark
const fs = require('fs');
const sharp = require('sharp');
const axios = require('axios');
const URL =
'http://static.megaphone.fm/podcasts/05f71746-a825-11e5-aeb5-a7a572df575e/image/uploads_2F1516902193862-jqkml22bswo-cee641b4533ddb31a5a7ab656fe45116_2FCURRENT_Reply+All+Logo.png';
function time(hrtime) {
const nanoseconds = hrtime[0] * 1e9 + hrtime[1];
const seconds = nanoseconds / 1e9;
return seconds;
}
async function request(url) {
const response = await axios({
url,
responseType: 'arraybuffer',
responseEncoding: null
});
return response.data;
}
(async () => {
const buffer = await request(URL);
const start1 = process.hrtime();
const result1 = await sharp(buffer, { density: 300 })
.resize(1920, 1920)
.max()
.toFormat('jpeg')
.toBuffer();
console.info('Without blur:', time(process.hrtime(start1)), 'seconds');
fs.writeFileSync('result-without-blur.jpg', result1);
const start2 = process.hrtime();
const result2 = await sharp(buffer, { density: 300 })
.resize(1920, 1920)
.max()
.blur(120)
.toFormat('jpeg')
.toBuffer();
console.info('With blur:', time(process.hrtime(start2)), 'seconds');
fs.writeFileSync('result-with-blur.jpg', result2);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment