Skip to content

Commit f2126d0

Browse files
committed
Made it faster
1 parent a2eefc4 commit f2126d0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

debug/filesize.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Jason Mulligan <[email protected]>
3434
* @module filesize
35-
* @version 1.6.3
35+
* @version 1.6.4
3636
*
3737
* @param {Mixed} arg String, Int or Float to transform
3838
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
@@ -43,7 +43,7 @@
4343
"use strict";
4444

4545
var filesize = function (arg) {
46-
var pos, short, num, sizes, size, result, suffix, i, n, x, z;
46+
var pos, short, num, sizes, size, result, regex, suffix, i, n, x, z;
4747

4848
if (typeof arguments[2] !== "undefined") {
4949
pos = arguments[1];
@@ -56,24 +56,20 @@
5656
short = (short === true);
5757
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
5858
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
59-
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}];
59+
sizes = ["B:0", "KB:1024", "MB:1048576", "GB:1073741824", "TB:1099511627776"]
6060
i = sizes.length;
6161
result = "";
62+
regex = /\.(.*)/;
6263

6364
while (i--) {
64-
x = sizes[i];
65-
for (n in x) {
66-
if (x.hasOwnProperty(n)) {
67-
size = x[n];
68-
suffix = n
69-
break;
70-
}
71-
}
65+
x = sizes[i].split(":");
66+
size = parseInt(x[1]);
67+
suffix = x[0];
7268
if (num >= size) {
7369
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
7470
if (short) {
7571
suffix = suffix.slice(0, 1);
76-
z = /\.(.*)/.exec(result);
72+
z = regex.exec(result);
7773
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
7874
}
7975
result += suffix;

production/filesize.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
*
3333
* @author Jason Mulligan <[email protected]>
3434
* @module filesize
35-
* @version 1.6.3
35+
* @version 1.6.4
3636
*
3737
* @param {Mixed} arg String, Int or Float to transform
3838
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
3939
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
4040
* @return {String} Readable file size String
4141
*/
42-
(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k,l;typeof arguments[2]!="undefined"?(b=arguments[1],c=arguments[2]):typeof arguments[1]=="boolean"?c=arguments[1]:b=arguments[1];if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");c=c===!0,b=c?1:typeof b=="undefined"?2:parseInt(b),d=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),e=[{B:0},{KB:1024},{MB:1048576},{GB:1073741824},{TB:1099511627776}],i=e.length,g="";while(i--){k=e[i];for(j in k)if(k.hasOwnProperty(j)){f=k[j],h=j;break}if(d>=f){g=(h==="B"?d:d/f).toFixed(b),c&&(h=h.slice(0,1),l=/\.(.*)/.exec(g),l!==null&&typeof l[1]!="undefined"&&l[1]==="0"&&(g=parseInt(g))),g+=h;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define("filesize",function(){return b});break;default:a.filesize=b}})(this)
42+
(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k,l,m;typeof arguments[2]!="undefined"?(b=arguments[1],c=arguments[2]):typeof arguments[1]=="boolean"?c=arguments[1]:b=arguments[1];if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");c=c===!0,b=c?1:typeof b=="undefined"?2:parseInt(b),d=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),e=["B:0","KB:1024","MB:1048576","GB:1073741824","TB:1099511627776"],j=e.length,g="",h=/\.(.*)/;while(j--){l=e[j].split(":"),f=parseInt(l[1]),i=l[0];if(d>=f){g=(i==="B"?d:d/f).toFixed(b),c&&(i=i.slice(0,1),m=h.exec(g),m!==null&&typeof m[1]!="undefined"&&m[1]==="0"&&(g=parseInt(g))),g+=i;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define("filesize",function(){return b});break;default:a.filesize=b}})(this)

0 commit comments

Comments
 (0)