有没有比使用if / then语句更好的解决方案呢?

时间:2013-08-09 04:00:42

标签: javascript performance if-statement

我有这个扩展检测功能,但它很大程度上依赖于if / then语句的多行。我可以使用不同的方法来获得相同的结果吗? (所有变量都在更高级别定义)。我想处理更少的代码,并提高性能速度。

if (extT == 'app') { docType = 'app'; docDesc = 'Application'; }
        else if (extT == 'exe') { docType = 'exe'; docDesc = 'Executable File'; }
        else if (extT == 'vwx') { docType = 'vwx'; docDesc = 'Vectorworks File'; }
        else if (extT == 'torrent') { docType = 'torrent'; docDesc = 'BitTorrent Document'; }
        else if (extT == 'ipsw') { docType = 'ipsw'; docDesc = 'Apple Device Software Update File'; }
        else if (extT == 'mpkg') { docType = 'mpkg'; docDesc = 'Installer package'; }
        else if (extT == 'prefPane') { docType = 'prefPane'; docDesc = 'Mac OS X Preference Pane'; }
        else if (extT == 'ogg') { docType = 'ogg_audio'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'ogv') { docType = 'ogv'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'flac') { docType = 'flac_audio'; docDesc = 'FLAC Audio File'; }
        else if (extT == 'ttf') { docType = 'ttf'; docDesc = 'TrueType Font'; }
        else if (extT == 'otf') { docType = 'otf'; docDesc = 'OpenType Font'; }
        else if (extT == 'air') { docType = 'air'; docDesc = 'Rich Internet Application'; }
        else if (extT == 'csproj') { docType = 'csproj'; docDesc = '.NET Source Code'; }
        else if (extT == 'download') { docType = 'download'; docDesc = 'File Download'; }
        else if (extT == 'log') { docType = 'log'; docDesc = 'Log File'; }
        else if (extT == 'conf') { docType = 'conf'; docDesc = 'IRC Related Configuration File'; }
        else if (extT == 'command') { docType = 'command'; docDesc = 'Terminal Shell Script'; }
        else if (extT == 'plist') { docType = 'plist'; docDesc = 'Property List XML File'; }
        else if (extT == 'mobileprovision') { docType = 'Developer Provisioning Profile'; docDesc = ''; }
        else if (extT == 'ds_store') { docType = 'DS_Store'; docDesc = 'Desktop Services Store'; }
        else if (extT == 'asp') { docType = 'asp'; docDesc = 'Active Server Pages'; }
        else if (extT == 'safariextz') { docType = 'safariextz'; docDesc = 'Safari Extension'; }
        else if (extT == 'qlab2license') { docType = 'QLab2License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlablicense') { docType = 'QLabLicense'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab1license') { docType = 'QLab1License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab3license') { docType = 'QLab3License'; docDesc = 'QLab License File'; }
        else if (extT == 'saver') { docType = 'saver'; docDesc = 'Mac OS X Preference Screen Saver'; }
        else if (extT == 'pkg') { docType = 'pkg'; docDesc = 'Installer Package'; }
        else if (extT == 'w3x') { docType = 'w3x'; docDesc = 'Warcraft III: The Frozen Throne'; }
        else if (extT == 'fcp') { docType = 'fcp'; docDesc = 'Final Cut Project File'; }
        else if (extT == 'pages') { docType = 'pages'; docDesc = 'Pages Publication'; }
        else if (extT == 'numbers') { docType = 'numbers'; docDesc = 'Numbers Document'; }
        else if (extT == 'key') { docType = 'key'; docDesc = 'Keynote Document'; }
        else if (extT == 'wwf') { docType = 'wwf'; docDesc = 'WWF Non-printable Document'; }
        else if (extT == 'swd') { docType = 'swd'; docDesc = 'Shockwave Document'; }
        else if (extT == 'rtfd') { docType = 'rtfd'; docDesc = 'NeXT RTF-Packet'; }
        else if (extT == 'wtpl') { docType = 'wtpl'; docDesc = 'WireTap Studio Library'; }
        else if (extT == 'abbu') { docType = 'abbu'; docDesc = 'Address Book Archive File'; }
        else if (extT == 'thllibrary') { docType = 'thllibrary'; docDesc = 'The Hit List Library'; }
        else if (extT == 'logic') { docType = 'logic'; docDesc = 'Logic Project File'; }
        else if (extT == 'pat') { docType = 'pat'; docDesc = 'Adobe Photoshop Patterns File'; }
        else if (extT == 'dwg') { docType = 'dwg'; docDesc = 'AutoCAD Image'; }
        else if (extT == 'ctb') { docType = 'ctb'; docDesc = 'AutoCAD Color-Based Plot Style File'; }
        else if (extT == 'stb') { docType = 'stb'; docDesc = 'AutoCAD Plot Style Table File'; }
        else if (extT == 'lck') { docType = 'lck'; docDesc = 'Lock File'; }
        else if (extT == 'aep') { docType = 'aep'; docDesc = 'After Effects Project'; }
        else if (extT == 'prtl') { docType = 'prtl'; docDesc = 'Adobe Permiere Title File'; }
        else if (extT == 'prproj') { docType = 'prproj'; docDesc = 'Adobe Premiere Project'; }
        else if (extT == 'band') { docType = 'band'; docDesc = 'GarageBand Project File'; }
        else if (extT == 'h') { docType = 'h'; docDesc = 'Header File'; }
        else if (extT == 'c') { docType = 'c'; docDesc = 'C/C++ Program File'; }
        else if (extT == 'r') { docType = 'r'; docDesc = 'Rez Source Code File'; }
        else if (extT == 'xcodeproj') { docType = 'xcodeproj'; docDesc = 'XCode Project'; }
        else if (extT == 'xcconfig') { docType = 'xcconfig'; docDesc = 'Xcode Configuration Settings'; }
        else if (extT == 'rcproject') { docType = 'rcproject'; docDesc = 'iMovie Project'; }
        else if (extT == 'cache') { docType = 'cache'; docDesc = 'Cache File'; }
        else if (extT == 'exs') { docType = 'exs'; docDesc = 'EXS24 instrument file'; }
        else if (extT == 'sdir') { docType = 'SDIR'; docDesc = 'Space Designer Impulse Response'; }
        else if (extT == 'ovw') { docType = 'ovw'; docDesc = 'Cool Edit Pro Overview File'; }
        else if (extT == 'lwp') { docType = 'lwp'; docDesc = 'LightWright Project'; }
        else if (extT == 'bin') { docType = 'BIN'; docDesc = 'Binary File'; }
        else if (extT == 'ini') { docType = 'INI'; docDesc = 'Initialization/Configuration File'; }
        else if (extT == 'ex_') { docType = 'EX_'; docDesc = 'Compressed EXE File'; }
        else if (extT == 'bat') { docType = 'bat'; docDesc = 'Microsoft Batch File'; }
        else if (extT == 'edml') { docType = 'edml'; docDesc = 'Adobe Extension Data Markup Language (EDML) Document'; }
        else if (extT == 'mak') { docType = 'MAK'; docDesc = 'MakeFile'; }
        else if (extT == 'cfm') { docType = 'cfm'; docDesc = 'Adobe ColdFusion Document'; }
        else if (extT == 'indd') { docType = 'indd'; docDesc = 'Adobe InDesign Document'; }
        else if (extT == 'indt') { docType = 'indt'; docDesc = 'Adobe InDesign Template'; }
        else if (extT == 'asnd') { docType = 'asnd'; docDesc = 'Adobe Sound Document'; }
        else if (extT == 'cpp') { docType = 'cpp'; docDesc = 'C++ Source'; }
        else if (extT == 'aac') { docType = 'aac'; docDesc = 'MPEG-2 Advanced Audio Coding File'; }
        else if (extT == 'm2v') { docType = 'm2v'; docDesc = 'MPEG-2 Blu-ray File'; }
        else if (extT == 'olproj') { docType = 'olproj'; docDesc = 'Adobe OnLocation Project'; }
        else if (extT == 'ipa') { docType = 'ipa'; docDesc = 'iOS Application'; }
        else if (extT == 'apk') { docType = 'apk'; docDesc = 'Android Application'; }
        else if (extT == 'scpt') { docType = 'scpt'; docDesc = 'Script File'; }
        else if (extT == 'applescript') { docType = 'AppleScript'; docDesc = 'AppleScript File'; }
        else if (extT == 'emlxpart') { docType = 'emlxpart'; docDesc = 'Mail Message Attachment'; }
        else if (extT == 'emlx') { docType = 'emlx'; docDesc = 'Mail Message'; }
        else if (extT == 'webbookmark') { docType = 'webbookmark'; docDesc = 'Safari Bookmark'; }
        else if (extT == 'jsx') { docType = 'jsx'; docDesc = 'Adobe JavaScript File'; }
        else if (extT == 'icns') { docType = 'icns'; docDesc = 'Apple Icon Image'; }
        else if (extT == 'caf') { docType = 'caf_audio'; docDesc = 'Apple Loop'; }
        else if (extT == 'cues') { docType = 'cues'; docDesc = 'QLab Workspace'; }
        else { docType = '--'; }

3 个答案:

答案 0 :(得分:11)

将对象用作地图。

var map = {
    "exe": {type:"exe",desc:"Executable File"},

    "vwx": {type:"vwx",desc:"Vectorworks File"}
};

var extT = "exe";

var docType = map[extT].type;
var docDesc = map[extT].desc;

http://jsfiddle.net/ZfbHf/1/

优点

  • 将数据列为实际数据,然后仅包含2行分配
  • 可以从JSON文件加载数据,将其从代码中完全删除并使其可配置而无需更改代码
  • 清晰的数据结构
  • 明确的逻辑很容易理解(没有人冒险在其中一个if语句中添加随机代码行使其难以调试)

答案 1 :(得分:2)

看起来不像代码少,但如果你使用一个对象来创建一个地图,它应该更有效:

var types = {
    'app': { 'docType': 'app', 'docDesc': 'Application' }, 
    'exe': { 'docType': 'exe', 'docDesc': 'Executable File' }, 
    ...
};

然后将其用作查找:

var curType = types['app'];

基本上所有@ ben336都说 - 用他的答案

答案 2 :(得分:0)

可以使用case / switch语句,但你基本上会在同一条船上,并且(据我所知)两者之间确实没有性能差异。然后,我将基于C#知识的最后陈述作为基础。也许将这个信息预加载到某种数组中,从数据库或其他东西中取出它?这样你就可以根据传递给它的参数调用它,只需返回你需要的值。

目前还不清楚除了javascript之外你使用的是什么平台,所以在给出答案时不能真正做得太多。我的价值2美分,至少......

注意 的 看看Ben336和dc5的答案,这看起来也是可行的选择。