1. /**
  2. * Configurable Data Splitter for Development Use (Memory Efficient)
  3. *
  4. * This function takes a configuration object and splits it into smaller,
  5. * manageable chunks, suitable for development environments with limited
  6. * memory. It prioritizes avoiding large single objects.
  7. *
  8. * @param {object} config - The configuration object to split.
  9. * @param {number} chunkSize - The maximum size (in key-value pairs) of each chunk.
  10. * @returns {Array<object>} An array of configuration chunks.
  11. */
  12. function splitConfig(config, chunkSize) {
  13. const chunks = [];
  14. let currentChunk = {};
  15. let chunkIndex = 0;
  16. for (const key in config) {
  17. if (config.hasOwnProperty(key)) {
  18. const value = config[key];
  19. if (Object.keys(currentChunk).length < chunkSize) {
  20. currentChunk[key] = value;
  21. } else {
  22. chunks.push(currentChunk); // Push the full chunk
  23. currentChunk = { [key]: value }; // Start a new chunk
  24. chunkIndex++;
  25. }
  26. }
  27. }
  28. if (Object.keys(currentChunk).length > 0) {
  29. chunks.push(currentChunk); // Push the last chunk if it's not empty
  30. }
  31. return chunks;
  32. }
  33. //Example Usage:
  34. // const configData = {
  35. // "setting1": "value1",
  36. // "setting2": 123,
  37. // "setting3": true,
  38. // "setting4": [1,2,3],
  39. // "setting5": {nested: "value"}
  40. // };
  41. // const chunkedConfig = splitConfig(configData, 2);
  42. // console.log(chunkedConfig);

Add your comment