Skip to content

Remove lodash dependencies + Optimization #130

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

Merged
merged 13 commits into from
Jun 9, 2025

Conversation

Genarito
Copy link
Contributor

First of all, thank you for this library! I've been using it in several of my projects.

The main purpose of this PR is to replace the usage of Lodash modules with native JS functions. This not only resolves the installation warning reported in #128, but also reduces the bundle size of the library.

Additionally, while making these changes, I took the liberty to optimize the mergeObjectsAndSortKeys function by avoiding unnecessary object-to-array and array-to-object conversions. In the new approach, object merging is performed using a native method, followed by the use of the reduce function. According to the following benchmark, I found that the new implementation is approximately 77% faster than the previous one.

Benchmark
// Version 1: Sort using Object.keys (new implementation)
function sortWithObjectKeys(obj) {
  const sortedKeys = Object.keys(obj).sort();
  return sortedKeys.reduce((sortedObj, key) => {
    sortedObj[key] = obj[key];
    return sortedObj;
  }, {});
}

// Version 2: Sort using toPairs + fromPairs (Lodash-style, current implementation)
function toPairs(obj) {
  return Object.entries(obj);
}

function fromPairs(pairs) {
  return Object.fromEntries(pairs);
}

function sortWithPairs(obj) {
  const sortedPairs = toPairs(obj).sort((a, b) => a[0].localeCompare(b[0]));
  return fromPairs(sortedPairs);
}

// Generate a large object with random keys
function generateRandomObject(size = 10000) {
  const obj = {};
  for (let i = 0; i < size; i++) {
    const key = Math.random().toString(36).substring(2, 10);
    obj[key] = i;
  }
  return obj;
}

// Benchmark runner
function runBenchmark() {
  const iterations = 10;
  const sampleObject = generateRandomObject();

  console.log(`Benchmarking with ${Object.keys(sampleObject).length} properties`);

  console.time('sortWithObjectKeys');
  for (let i = 0; i < iterations; i++) {
    sortWithObjectKeys(sampleObject);
  }
  console.timeEnd('sortWithObjectKeys');

  console.time('sortWithPairs');
  for (let i = 0; i < iterations; i++) {
    sortWithPairs(sampleObject);
  }
  console.timeEnd('sortWithPairs');
}

runBenchmark();

Summary of changes:

  • Removed Lodash dependencies, replaced with native Javascript implementations.
  • Optimized the mergeObjectsAndSortKeys function for better performance.
  • Added the cross-env library (development only) to improve cross-platform compatibility in npm scripts.

I hope you find these changes useful and that we can continue improving the library together. I'm available for any adjustments or improvements you might suggest.

Thank you for your time! Best regards! 😄

@Genarito
Copy link
Contributor Author

Genarito commented Jun 5, 2025

Hi @fjsj @rvlb how are you doing? Could you check please if this PR is useful for your project?

@fjsj fjsj requested a review from rvlb June 6, 2025 13:37
@fjsj
Copy link
Member

fjsj commented Jun 6, 2025

thanks for the PR @Genarito!
we'll review soon

@rvlb rvlb merged commit faed359 into django-webpack:master Jun 9, 2025
3 checks passed
@Genarito Genarito deleted the remove-lodash-optimizations branch June 9, 2025 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants