Logo

dev-resources.site

for different kinds of informations.

Lighter LoDash Builds for Laravel

Published at
1/5/2021
Categories
laravel
lodash
performance
Author
Mike Healy
Categories
3 categories in total
laravel
open
lodash
open
performance
open
Lighter LoDash Builds for Laravel

Laravel projects often include the LoDash JavaScript utility library. It contains handy utilities, however if you don't need its entire suite you may be serving unnecessary JS to your users that you never run.

LoDash supports custom builds to avoid this problem, and it's fairly easy to modify your Laravel's bootstrap.js file trim down the features you're loading.

Default resources/js/bootstrap.js

window._ = require('lodash'); // 71.1K (gzipped: 24.6K)

Customizing the build

In my case I was only using the throttle and debounce helpers from LoDash. This can be mapped to the window lodash object (typically _) like so:

window._ = require('lodash/core')  // 14K (gzipped: 5.3K)
window._.debounce = require('lodash/debounce')  // 3.5K (gzipped: 1.4K)
window._.throttle = require('lodash/throttle') // 3.7K (gzipped: 1.4K)

My application JS can then call _.debounce() and _.throttle() for those functions with a significant saving to my JS bundle size.

Featured ones: