Skip to content

Commit

Permalink
[js] performance tuning that replace $.map() to $.grep()
Browse files Browse the repository at this point in the history
  • Loading branch information
nao-pon committed Dec 21, 2017
1 parent 08f801a commit 6c989ec
Show file tree
Hide file tree
Showing 29 changed files with 128 additions and 123 deletions.
4 changes: 2 additions & 2 deletions js/commands/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ elFinder.prototype.commands.archive = function() {
this.getstate = function(select) {
var sel = this.files(select),
cnt = sel.length,
chk = (cnt && ! fm.isRoot(sel[0]) && (fm.file(sel[0].phash) || {}).write && ! $.map(sel, function(f){ return f.read ? null : true; }).length),
chk = (cnt && ! fm.isRoot(sel[0]) && (fm.file(sel[0].phash) || {}).write && ! $.grep(sel, function(f){ return f.read ? false : true; }).length),
cwdId;

if (chk && fm.searchStatus.state > 1) {
cwdId = fm.cwd().volumeid;
chk = (cnt === $.map(sel, function(f) { return f.read && f.hash.indexOf(cwdId) === 0 ? f : null; }).length);
chk = (cnt === $.grep(sel, function(f) { return f.read && f.hash.indexOf(cwdId) === 0 ? true : false; }).length);
}

return chk && !this._disabled && mimes.length && (cnt || (dfrd && dfrd.state() == 'pending')) ? 0 : -1;
Expand Down
4 changes: 2 additions & 2 deletions js/commands/chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ elFinder.prototype.commands.chmod = function() {
this.checkstate = function(sel) {
var cnt = sel.length;
if (!cnt) return false;
var chk = $.map(sel, function(f) {
return (f.isowner && f.perm && isPerm(f.perm) && (cnt == 1 || f.mime != 'directory')) ? f : null;
var chk = $.grep(sel, function(f) {
return (f.isowner && f.perm && isPerm(f.perm) && (cnt == 1 || f.mime != 'directory')) ? true : false;
}).length;
return (cnt == chk)? true : false;
};
Expand Down
2 changes: 1 addition & 1 deletion js/commands/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ elFinder.prototype.commands.copy = function() {
var sel = this.files(select),
cnt = sel.length;

return cnt && $.map(sel, function(f) { return f.read ? f : null; }).length == cnt ? 0 : -1;
return cnt && $.grep(sel, function(f) { return f.read ? true : false; }).length == cnt ? 0 : -1;
};

this.exec = function(hashes) {
Expand Down
2 changes: 1 addition & 1 deletion js/commands/cut.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ elFinder.prototype.commands.cut = function() {
var sel = this.files(select),
cnt = sel.length;

return cnt && $.map(sel, function(f) { return f.read && ! f.locked && ! fm.isRoot(f) ? f : null; }).length == cnt ? 0 : -1;
return cnt && $.grep(sel, function(f) { return f.read && ! f.locked && ! fm.isRoot(f) ? true : false; }).length == cnt ? 0 : -1;
};

this.exec = function(hashes) {
Expand Down
10 changes: 5 additions & 5 deletions js/commands/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ elFinder.prototype.commands.download = function() {

if (mixed) {
mixedCmd = czipdl? 'zipdl' : 'download';
hashes = $.map(hashes, function(h) {
hashes = $.grep(hashes, function(h) {
var f = fm.file(h),
res = (! f || (! czipdl && f.mime === 'directory') || ! fm.isCommandEnabled(mixedCmd, h))? null : h;
res = (! f || (! czipdl && f.mime === 'directory') || ! fm.isCommandEnabled(mixedCmd, h))? false : true;
if (f && inExec && ! res) {
$('#' + fm.cwdHash2Id(f.hash)).trigger('unselect');
}
Expand All @@ -50,8 +50,8 @@ elFinder.prototype.commands.download = function() {
}
}

return $.map(self.files(hashes), function(f) {
var res = (! f.read || (! zipOn && f.mime == 'directory')) ? null : f;
return $.grep(self.files(hashes), function(f) {
var res = (! f.read || (! zipOn && f.mime == 'directory')) ? false : true;
if (inExec && ! res) {
$('#' + fm.cwdHash2Id(f.hash)).trigger('unselect');
}
Expand Down Expand Up @@ -317,7 +317,7 @@ elFinder.prototype.commands.download = function() {
return dfrd.reject();
}

fileCnt = $.map(files, function(f) { return f.mime === 'directory'? null : true; }).length;
fileCnt = $.grep(files, function(f) { return f.mime === 'directory'? false : true; }).length;
link = $('<a>').hide().appendTo('body');
html5dl = (typeof link.get(0).download === 'string');

Expand Down
2 changes: 1 addition & 1 deletion js/commands/duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ elFinder.prototype.commands.duplicate = function() {
var sel = this.files(select),
cnt = sel.length;

return cnt && fm.cwd().write && $.map(sel, function(f) { return f.read && f.phash === fm.cwd().hash && ! fm.isRoot(f)? f : null; }).length == cnt ? 0 : -1;
return cnt && fm.cwd().write && $.grep(sel, function(f) { return f.read && f.phash === fm.cwd().hash && ! fm.isRoot(f)? true : false; }).length == cnt ? 0 : -1;
};

this.exec = function(hashes) {
Expand Down
16 changes: 8 additions & 8 deletions js/commands/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ elFinder.prototype.commands.edit = function() {
mime = files[0].mime;
ext = files[0].name.replace(/^.*(\.[^.]+)$/, '$1');
}
return $.map(files, function(file) {
return $.grep(files, function(file) {
var res;
if (skip) {
return null;
return false;
}
res = (fm.mimeIsText(file.mime) || $.inArray(file.mime, cnt === 1? mimesSingle : mimes) !== -1)
&& (!self.onlyMimes.length || $.inArray(file.mime, self.onlyMimes) !== -1)
&& file.read && file.write
&& (cnt === 1 || (file.mime === mime && file.name.substr(ext.length * -1) === ext))
&& fm.uploadMimeCheck(file.mime, file.phash)? file : null;
&& fm.uploadMimeCheck(file.mime, file.phash)? true : false;
if (!res) {
skip = true;
}
Expand Down Expand Up @@ -670,11 +670,11 @@ elFinder.prototype.commands.edit = function() {

dfd.always(function() {
if (ccData) {
opts.editors = $.map(opts.editors, function(e) {
opts.editors = $.grep(opts.editors, function(e) {
if (e.info && e.info.cmdCheck) {
return ccData[e.info.cmdCheck]? e : null;
return ccData[e.info.cmdCheck]? true : false;
} else {
return e;
return true;
}
});
}
Expand All @@ -695,8 +695,8 @@ elFinder.prototype.commands.edit = function() {
mimesSingle = ($.uniqueSort || $.unique)(mimesSingle);
mimes = ($.uniqueSort || $.unique)(mimes);

opts.editors = $.map(opts.editors, function(e) {
return e.disabled? null : e;
opts.editors = $.grep(opts.editors, function(e) {
return e.disabled? false : true;
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion js/commands/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ elFinder.prototype.commands.empty = function() {
cnt;

cnt = sel.length;
return $.map(sel, function(f) { return f.write && f.mime === 'directory' ? f : null; }).length == cnt ? 0 : -1;
return $.grep(sel, function(f) { return f.write && f.mime === 'directory' ? true : false; }).length == cnt ? 0 : -1;
};

this.exec = function(hashes) {
Expand Down
9 changes: 6 additions & 3 deletions js/commands/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ elFinder.prototype.commands.extract = function() {
fm = self.fm,
mimes = [],
filter = function(files) {
return $.map(files, function(file) {
return file.read && $.inArray(file.mime, mimes) !== -1 ? file : null;
return $.grep(files, function(file) {
return file.read && $.inArray(file.mime, mimes) !== -1 ? true : false;
});
};

Expand Down Expand Up @@ -50,7 +50,10 @@ elFinder.prototype.commands.extract = function() {

var names = $.map(fm.files(hashes), function(file) { return file.name; });
var map = {};
$.map(fm.files(hashes), function(file) { map[file.name] = file; });
$.grep(fm.files(hashes), function(file) {
map[file.name] = file;
return false;
});

var decide = function(decision) {
switch (decision) {
Expand Down
4 changes: 2 additions & 2 deletions js/commands/getfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
filter = function(files) {
var o = self.options;

files = $.map(files, function(file) {
return (file.mime != 'directory' || o.folders) && file.read ? file : null;
files = $.grep(files, function(file) {
return (file.mime != 'directory' || o.folders) && file.read ? true : false;
});

return o.multiple || files.length == 1 ? files : [];
Expand Down
4 changes: 2 additions & 2 deletions js/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@
forms.toolbarPref && (forms.toolbarPref = (function() {
var node = $('<div/>');
init(function() {
var pnls = $.map(fm.options.uiOptions.toolbar, function(v) {
return $.isArray(v)? v : null;
var pnls = $.grep(fm.options.uiOptions.toolbar, function(v) {
return $.isArray(v)? true : false;
}),
tags = [],
hides = fm.storage('toolbarhides') || {};
Expand Down
6 changes: 3 additions & 3 deletions js/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
if (o.custom) {
$.each(o.custom, function(name, details) {
if (
(!details.mimes || $.map(details.mimes, function(m){return (file.mime === m || file.mime.indexOf(m+'/') === 0)? true : null;}).length)
(!details.mimes || $.grep(details.mimes, function(m){return (file.mime === m || file.mime.indexOf(m+'/') === 0)? true : false;}).length)
&&
(!details.hashRegex || file.hash.match(details.hashRegex))
) {
Expand All @@ -207,7 +207,7 @@
} else {
view = view.replace('{class}', 'elfinder-cwd-icon-group');
title = tpl.groupTitle.replace('{items}', msg.items).replace('{num}', cnt);
dcnt = $.map(files, function(f) { return f.mime == 'directory' ? 1 : null ; }).length;
dcnt = $.grep(files, function(f) { return f.mime == 'directory' ? true : false ; }).length;
if (!dcnt) {
size = 0;
$.each(files, function(h, f) {
Expand All @@ -222,7 +222,7 @@
content.push(row.replace(l, msg.kind).replace(v, msg.files));
content.push(row.replace(l, msg.size).replace(v, fm.formatSize(size)));
} else {
rdcnt = $.map(files, function(f) { return f.mime === 'directory' && (! f.phash || f.isroot)? 1 : null ; }).length;
rdcnt = $.grep(files, function(f) { return f.mime === 'directory' && (! f.phash || f.isroot)? true : false ; }).length;
dcnt -= rdcnt;
content.push(row.replace(l, msg.kind).replace(v, (rdcnt === cnt || dcnt === cnt)? msg[rdcnt? 'roots' : 'folders'] : $.map({roots: rdcnt, folders: dcnt, files: cnt - rdcnt - dcnt}, function(c, t) { return c? msg[t]+' '+c : null; }).join(', ')));
content.push(row.replace(l, msg.size).replace(v, tpl.spinner.replace('{text}', msg.calc).replace('{name}', 'size')));
Expand Down
2 changes: 1 addition & 1 deletion js/commands/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ elFinder.prototype.commands.mkdir = function() {
if (curOrg === 'navbar') {
return cnt && sel[0].write && sel[0].read? 0 : -1;
} else {
return cwd.write && (!cnt || $.map(sel, function(f) { return f.read && ! f.locked? f : null; }).length == cnt)? 0 : -1;
return cwd.write && (!cnt || $.grep(sel, function(f) { return f.read && ! f.locked? true : false; }).length == cnt)? 0 : -1;
}
};

Expand Down
4 changes: 2 additions & 2 deletions js/commands/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

return cnt == 1
? (sel[0].read? 0 : -1)
: (cnt && !this.fm.UA.Mobile) ? ($.map(sel, function(file) { return file.mime == 'directory' || ! file.read ? null : file;}).length == cnt ? 0 : -1) : -1;
: (cnt && !this.fm.UA.Mobile) ? ($.grep(sel, function(file) { return file.mime == 'directory' || ! file.read ? false : true;}).length == cnt ? 0 : -1) : -1;
};

this.exec = function(hashes, cOpts) {
Expand Down Expand Up @@ -55,7 +55,7 @@
});
}

files = $.map(files, function(file) { return file.mime != 'directory' ? file : null; });
files = $.grep(files, function(file) { return file.mime != 'directory' ? true : false; });

// nothing to open or files and folders selected - do nothing
if (cnt != files.length) {
Expand Down
12 changes: 6 additions & 6 deletions js/commands/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ elFinder.prototype.commands.paste = function() {
}
} else {
existedArr = [];
existed = $.map(names, function(n) {
existed = $.grep(names, function(n) {
if (typeof n === 'string') {
return n;
return true;
} else {
// support to >=2.1.11 plugin Normalizer, Sanitizer
existedArr = existedArr.concat(n);
return null;
return false;
}
});
if (existedArr.length) {
Expand All @@ -181,11 +181,11 @@ elFinder.prototype.commands.paste = function() {
},
paste = function(selFiles) {
var renames = [],
files = $.map(selFiles, function(file) {
files = $.grep(selFiles, function(file) {
if (file.rename) {
renames.push(file.name);
}
return !file.remove ? file : null;
return !file.remove ? true : false;
}),
cnt = files.length,
groups = {},
Expand Down Expand Up @@ -338,7 +338,7 @@ elFinder.prototype.commands.paste = function() {
fparents.pop();
if ($.inArray(dst.hash, fparents) !== -1) {

if ($.map(fparents, function(h) { var d = fm.file(h); return d.phash == dst.hash && d.name == file.name ? d : null; }).length) {
if ($.grep(fparents, function(h) { var d = fm.file(h); return d.phash == dst.hash && d.name == file.name ? true : false; }).length) {
return !dfrd.reject(['errReplByChild', file.name]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/commands/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ elFinder.prototype.commands.places = function() {
var self = this,
fm = this.fm,
filter = function(hashes) {
return $.map(self.files(hashes), function(f) { return f.mime == 'directory' ? f : null; });
return $.grep(self.files(hashes), function(f) { return f.mime == 'directory' ? true : false; });
},
places = null;

Expand Down
8 changes: 4 additions & 4 deletions js/commands/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ elFinder.prototype.commands.rename = function() {
mime = sel[0].mime;
}

state = ((cnt === 1 && !sel[0].locked && !fm.isRoot(sel[0])) || (fm.api > 2.1030 && cnt === $.map(sel, function(f) {
state = ((cnt === 1 && !sel[0].locked && !fm.isRoot(sel[0])) || (fm.api > 2.1030 && cnt === $.grep(sel, function(f) {
if (!brk && !f.locked && f.phash === phash && !fm.isRoot(f) && (mime === f.mime || ext === fm.splitFileExtention(f.name)[1].toLowerCase())) {
return f;
return true;
} else {
brk = true;
return null;
brk && (brk = true);
return false;
}
}).length)) ? 0 : -1;

Expand Down
2 changes: 1 addition & 1 deletion js/commands/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@

this.getstate = function(sel, e) {
sel = sel || fm.selected();
return sel.length && $.map(sel, function(h) {var f = fm.file(h); return f && ! f.locked && ! fm.isRoot(f)? h : null; }).length == sel.length
return sel.length && $.grep(sel, function(h) {var f = fm.file(h); return f && ! f.locked && ! fm.isRoot(f)? true : false; }).length == sel.length
? 0 : -1;
};

Expand Down
8 changes: 4 additions & 4 deletions js/commands/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ elFinder.prototype.commands.rm = function() {
if (Object.keys(res).length) {
if (err.length > 1) {
if (res.removed || res.removed.length) {
hashes = $.map(targets, function(h) {
return $.inArray(h, res.removed) === -1? h : null;
hashes = $.grep(targets, function(h) {
return $.inArray(h, res.removed) === -1? true : false;
});
}
if (hashes.length) {
Expand Down Expand Up @@ -350,7 +350,7 @@ elFinder.prototype.commands.rm = function() {
if (targets && targets.length) {
if (targets.length > 1 && fm.searchStatus.state === 2) {
root1st = fm.file(fm.root(targets[0])).volumeid;
if (!$.map(targets, function(h) { return h.indexOf(root1st) !== 0? 1 : null ; }).length) {
if (!$.grep(targets, function(h) { return h.indexOf(root1st) !== 0? true : false ; }).length) {
thash = fm.option('trashHash', targets[0]);
}
} else {
Expand Down Expand Up @@ -406,7 +406,7 @@ elFinder.prototype.commands.rm = function() {
this.getstate = function(select) {
var sel = this.hashes(select);

return sel.length && $.map(sel, function(h) { var f = fm.file(h); return f && ! f.locked && ! fm.isRoot(f)? h : null; }).length == sel.length
return sel.length && $.grep(sel, function(h) { var f = fm.file(h); return f && ! f.locked && ! fm.isRoot(f)? true : false; }).length == sel.length
? 0 : -1;
};

Expand Down
2 changes: 1 addition & 1 deletion js/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ elFinder.prototype.commands.search = function() {
mime = $.map(mime, function(m){
m = $.trim(m);
return m && ($.inArray(m, onlyMimes) !== -1
|| $.map(onlyMimes, function(om) { return m.indexOf(om) === 0? true : null; }).length
|| $.grep(onlyMimes, function(om) { return m.indexOf(om) === 0? true : false; }).length
)? m : null;
});
}
Expand Down
4 changes: 2 additions & 2 deletions js/commands/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ elFinder.prototype.commands.upload = function() {
},
getSelector = function() {
var hash = targetDir.hash,
dirs = $.map(fm.files(hash), function(f) {
return (f.mime === 'directory' && f.write)? f : null;
dirs = $.grep(fm.files(hash), function(f) {
return (f.mime === 'directory' && f.write)? true : false;
});

if (! dirs.length) {
Expand Down
2 changes: 1 addition & 1 deletion js/elFinder.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ elFinder.prototype.command = function(fm) {
*/
this.hashes = function(hashes) {
return hashes
? $.map(Array.isArray(hashes) ? hashes : [hashes], function(hash) { return fm.file(hash) ? hash : null; })
? $.grep(Array.isArray(hashes) ? hashes : [hashes], function(hash) { return fm.file(hash) ? true : false; })
: fm.selected();
};

Expand Down
Loading

0 comments on commit 6c989ec

Please sign in to comment.