简体   繁体   English

编写自己的解析器值得它带来额外的运行时吗?

[英]Is writing your own parser worth the extra runtime it causes?

I am doing work involving a lot of DOM manipulation. 我正在做很多涉及DOM操作的工作。 I wrote a function to more efficiently change common styles on objects using "hotkeys". 我编写了一个函数,可以使用“热键”更有效地更改对象的通用样式。 It simply interprets this: 它只是解释了这一点:

styles = parseStyles({p:"absolute",l:"100px",t:"20px",bg:"#CC0000"});

as this: 这样:

styles = {position:"absolute",left:"100px",top:"20px",background:"#CC0000"};

This came about mainly to save me from having to read so much, and because I wanted to see if I could do it :) (as well as file sizes). 这样做的主要目的是使我不必读太多书,并且因为我想看看是否可以这样做:)(以及文件大小)。 I find these hotkeys easier to look at; 我发现这些热键更易于查看; I am setting and resetting styles dozens of times for different custom DOM objects. 我为不同的自定义DOM对象设置和重置样式数十次。

But , is having a bottleneck like this going to be a significant burden on performance and runtime if my page is using it up to 5,000 times in a session, about 10-25 executions at a time? 但是 ,如果我的页面在一个会话中最多使用5,000次(每次执行约10-25次),是否会有这样的瓶颈对性能和运行时间造成重大负担?

function parseStyles(toParse){
    var stylesKey = 
        {h:"height",p:"position",l:"left",t:"top",r:"right",b:"bottom",bg:"background"}, 
        parsedStyles = {};
    for (entry in toParse){
        if (entry in stylesKey){
            parsedStyles[stylesKey[entry]] = toParse[entry];
        } else {
            parsedStyles[entry] = toParse[entry];
        }
    }
    return parsedStyles;
}

I find that setting non-computed styles is rarely ever needed any more. 我发现不再需要设置非计算样式。 If you know the styles ahead of time, define a class for that in your CSS and addClass or removeClass from the necessary objects. 如果您提前知道样式,请在CSS中为其定义一个类,然后从必要的对象中添加或删除类。 Your code is a lot more maintainable and all style-specific info is in your CSS files rather than your Javascript files. 您的代码更具可维护性,并且所有特定于样式的信息都位于CSS文件而不是Javascript文件中。 Pretty much, the only time I set formatting-style info directly on an object anymore is when I'm using computed positions with absolute positioning and even then, if I rack my brain hard enough, the positioning problem can usually be solved with plain CSS/HTML. 几乎,我唯一一次直接在对象上设置格式样式信息的时间是当我使用具有绝对定位的计算位置时,即使这样,如果我动脑筋,定位问题通常也可以使用普通CSS来解决。 / HTML。

The examples you cite in your question look like static styles which could all be done with a CSS rule and simply doing addClass to an object. 您在问题中引用的示例看起来像静态样式,这些样式都可以通过CSS规则完成,而只需对对象执行addClass。 Besides being cleaner, it should be a lot faster to execute too. 除了更清洁之外,执行起来也应该更快。

It looks like what you're doing is using run-time parsing in order to save development-time typing. 看来您正在执行的是使用运行时解析,以节省开发时的输入。 That's fine if you don't notice the performance difference. 如果您没有注意到性能差异,那很好。 Only you can know that for sure because only you can test your app in the environments that you need it to run (old browser, old CPU, stress usage). 只有您可以肯定地知道这一点,因为只有您可以在需要其运行的环境(旧浏览器,旧CPU,压力使用)中测试您的应用。 We can't answer that for you. 我们无法为您回答。 It would certainly be faster not to be doing run-time conversion for something that is known at development time. 如果不对开发时已知的内容进行运行时转换,肯定会更快。 Whether that speed difference is relevant depends upon a lot of details and app requirements you haven't disclosed (and may not have even seriously thought about) and could only be figured out with configuration testing. 速度差异是否有意义取决于您尚未披露(可能甚至没有认真考虑过)的许多细节和应用程序需求,只有通过配置测试才能弄清楚。

If it were me and I had any thoughts that maybe I was calling this so much that it might impact performance, I'd find it a lot less work to do a little extra typing (or search and replace) and not have to test the potential performance issues. 如果是我,并且我有任何想法,也许我这么称呼它可能会影响性能,那么我会发现做一些额外的键入(或搜索和替换)工作量大大减少,而不必测试潜在的性能问题。

Memoize your parsing function. 记住您的解析功能。

The simple fact is, that over some finite area of time, the number of actual styles, or full style strings that you will process will likely be quite small, and will also, likely, have a reasonable amount of duplication. 一个简单的事实是,在一定的时间范围内,您将要处理的实际样式或完整样式字符串的数量可能会非常少,并且可能还会有合理的重复量。

So, when you go to parse a style expression, you can do something simple like store the expression in a map, and check if you've seen it before. 因此,当您解析样式表达式时,您可以做一些简单的事情,例如将表达式存储在地图中,并检查您以前是否看过它。 If you have, return the result that you got before. 如果有,请返回之前获得的结果。

This will save you quite a bit of time when reuse is involved, and likely not cost you much time when it's not. 涉及重用时,这将为您节省大量时间,而在不涉及重用时,可能不会花费您很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM