-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstringMaxLength.js
45 lines (39 loc) · 1.07 KB
/
stringMaxLength.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//#!/usr/bin/env node
//20100507 [email protected]
//Calcula el tamaño máximo que puede tener una string
//uso: node stringMaxLength.js
function rnd (n) {
return Math.floor(Math.random()* n);
}
function rndStr (len) {
var str= "";
while (str.length < len) {
str+= String.fromCharCode(32+ Math.floor(Math.random()* 96));
}
str[0];
return str;
}
var baseMem;
var testStr= "";
var global= (function () { return this })();
var pool= [
rndStr(1024*1024),
rndStr(1024*1024)
];
if (global.process && global.process.memoryUsage) {
baseMem= global.process.memoryUsage().heapUsed;
}
(function loop () {
testStr+= pool[rnd(pool.length)];
var txt= "str.length -> "+ (testStr.length / 1e6).toFixed(1) + "e6 chars";
if (global.process && global.process.memoryUsage) {
var flatten= Math.random() < .1;
if (flatten) {
testStr[testStr.length-1];
}
txt+= ", heap \u2206 -> +"+ ((global.process.memoryUsage().heapUsed- baseMem) / (1024*1024)).toFixed(1)+ " MB";
if (flatten) txt+= " *** flattened";
}
console.log(txt);
setTimeout(loop, 99);
})();