-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(transformer-sharp): remove lodash as depdendency #7909
Conversation
Could we switch from lodash includes to array.prototype.includes ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes ) ? this extension object is getting quite verbose |
While this solution is a little more verbose, here are the benchmarks to show the speed improvements of the extension lookup. To execute on your machine, you can run:
Results on my machine:
Source code of the benchmark from the gist: const Benchmark = require("benchmark");
const suite = new Benchmark.Suite();
const supportedExtensionsArray = [`jpeg`, `jpg`, `png`, `webp`, `tif`, `tiff`];
const supportedExtensionsObject = {
jpeg: true,
jpg: true,
png: true,
webp: true,
tif: true,
tiff: true
};
suite
.add("includes() => true (first item)", function() {
supportedExtensionsArray.includes("jpeg");
})
.add("includes() => true (last item)", function() {
supportedExtensionsArray.includes("tiff");
})
.add("includes() => false", function() {
supportedExtensionsArray.includes("");
})
.add("object property access => true (first item)", function() {
supportedExtensionsObject["jpeg"];
})
.add("object property access => true (last item)", function() {
supportedExtensionsObject["tiff"];
})
.add("object property access => false", function() {
supportedExtensionsObject[""];
})
.on("start", function() {
console.log("Starting benchmark");
console.log();
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
const fastest = this.filter("fastest")
.map("name")
.map(name => `- ${name}`)
.join("\n");
console.log();
console.log("Fastest");
console.log(fastest);
})
.run({ async: true }); Happy to change this to |
I got beaten! :) What a way to make me eat my words, hehe :) Can't really argue with results, will merge this on next merging session :) Just out of curiosity - can you also run ES6 Map and Set while you are benchmarking stuff? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numbers don't lie, nice one @macklinu!
@pieh I updated the benchmark to include Map and Set - check out the results!
|
This PR refactors gatsby-transformer-sharp to lookup supported extensions via a hashmap instead of an array (a very minor performance improvement), and in the process, it looks like lodash can be removed as a dependency of that package, as this was the only usage I could find in that section of the codebase.