Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. JSON.unflatten = function(data) {
  2. "use strict";
  3. if (Object(data) !== data || Array.isArray(data))
  4. return data;
  5. var result = {}, cur, prop, parts, idx;
  6. for(var p in data) {
  7. cur = result, prop = "";
  8. parts = p.split(".");
  9. for(var i=0; i<parts.length; i++) {
  10. idx = !isNaN(parseInt(parts[i]));
  11. cur = cur[prop] || (cur[prop] = (idx ? [] : {}));
  12. prop = parts[i];
  13. }
  14. cur[prop] = data[p];
  15. }
  16. return result[""];
  17. }
  18. JSON.flatten = function(data) {
  19. var result = {};
  20. function recurse (cur, prop) {
  21. if (Object(cur) !== cur) {
  22. result[prop] = cur;
  23. } else if (Array.isArray(cur)) {
  24. for(var i=0, l=cur.length; i<l; i++)
  25. recurse(cur[i], prop ? prop+"."+i : ""+i);
  26. if (l == 0)
  27. result[prop] = [];
  28. } else {
  29. var isEmpty = true;
  30. for (var p in cur) {
  31. isEmpty = false;
  32. recurse(cur[p], prop ? prop+"."+p : p);
  33. }
  34. if (isEmpty)
  35. result[prop] = {};
  36. }
  37. }
  38. recurse(data, "");
  39. return result;
  40. }
  41.  
  42. function benchmark() {
  43. var tries = 2;
  44. var count = 5;
  45. var result = [];
  46. var time, temp, noOpt;
  47. for(var x=0; x<tries; x++) {
  48. noOpt = [];
  49. time = Date.now();
  50. for(var i=0; i<count; i++) {
  51. temp = JSON.parse(JSON.stringify(fillObj({}, 4)));
  52. touchObj(temp, noOpt);
  53. }
  54. time = Date.now() - time;
  55. result.push("Nested : " + noOpt.length + " : " + time);
  56. //
  57. noOpt = [];
  58. time = Date.now();
  59. for(var i=0; i<count; i++) {
  60. temp = JSON.parse(JSON.stringify(fillObj({}, 4)));
  61. temp = JSON.flatten(temp);
  62. touchObj(temp, noOpt);
  63. temp = JSON.unflatten(temp);
  64. }
  65. time = Date.now() - time;
  66. result.push("Flattened : " + noOpt.length + " : " + time);
  67. }
  68. alert(result.join("\n"));
  69. }
  70. function fillObj(obj, depth) {
  71. obj["foo0"] = 1;
  72. obj["bar0"] = false;
  73. obj["foo2"] = -99999;
  74. obj["bar2"] = true;
  75. obj["foo3"] = 10002525.10002525;
  76. obj["bar3"] = "test";
  77. obj["foo4"] = "hello world hello world hello world hello world";
  78. obj["bar4"] = [1,2,3,4,5,6,7,8,9,0];
  79. if(depth > 0) {
  80. obj["foo5"] = {};
  81. fillObj(obj["foo5"], depth-1);
  82. obj["bar5"] = [{},{},{},{},{}];
  83. for(var i=0; i<obj["bar5"].length; i++) {
  84. fillObj(obj["bar5"][i], depth-1);
  85. }
  86. }
  87. return obj;
  88. }
  89. function touchObj(obj, noOpt) {
  90. if(Array.isArray(obj)) {
  91. for(var i=0; i<obj.length; i++) {
  92. touchObj(obj[i], noOpt);
  93. }
  94. } else if(typeof obj === "object" && obj !== null) {
  95. for(var i in obj) {
  96. touchObj(obj[i], noOpt);
  97. }
  98. } else {
  99. noOpt.push(true);
  100. }
  101. return noOpt;
  102. }
  103. benchmark();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement