/**
* Configurable Data Splitter for Development Use (Memory Efficient)
*
* This function takes a configuration object and splits it into smaller,
* manageable chunks, suitable for development environments with limited
* memory. It prioritizes avoiding large single objects.
*
* @param {object} config - The configuration object to split.
* @param {number} chunkSize - The maximum size (in key-value pairs) of each chunk.
* @returns {Array<object>} An array of configuration chunks.
*/
function splitConfig(config, chunkSize) {
const chunks = [];
let currentChunk = {};
let chunkIndex = 0;
for (const key in config) {
if (config.hasOwnProperty(key)) {
const value = config[key];
if (Object.keys(currentChunk).length < chunkSize) {
currentChunk[key] = value;
} else {
chunks.push(currentChunk); // Push the full chunk
currentChunk = { [key]: value }; // Start a new chunk
chunkIndex++;
}
}
}
if (Object.keys(currentChunk).length > 0) {
chunks.push(currentChunk); // Push the last chunk if it's not empty
}
return chunks;
}
//Example Usage:
// const configData = {
// "setting1": "value1",
// "setting2": 123,
// "setting3": true,
// "setting4": [1,2,3],
// "setting5": {nested: "value"}
// };
// const chunkedConfig = splitConfig(configData, 2);
// console.log(chunkedConfig);
Add your comment