Created
December 1, 2014 05:39
-
-
Save elrasguno/a59dd4395d1e8a7e1d6a to your computer and use it in GitHub Desktop.
Aggregate multiple JSON strings into one to save on JSON.parse CPU consumption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Aggregate multiple JSON strings into one to save on CPU. | |
* | |
* @param {Array} jsonStringsArray An array of valid JSON strings to be parsed together. | |
* @returns {Array} An array of JavaScript Objects, or an empty array if any members of the input array are invalid JSON. | |
*/ | |
function aggregateParseJSON(jsonStringsArray) | |
{ | |
var tmpArray = '[' + jsonStringsArray.join(',') + ']', | |
parsed = []; | |
try { | |
parsed = JSON.parse(tmpArray); | |
} catch (e) {} | |
return parsed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment