CPU昂贵的JavaScript [英] CPU expensive javascript

查看:78
本文介绍了CPU昂贵的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划从 BBC新闻复制统计数据javascript。 ,但是我对该脚本的CPU使用率很感兴趣(在您按下开始按钮之后)。可以添加脚本那么昂贵吗?

I plan to replicate the statistics javascript from BBC News, but I'm intrigued by the CPU usage of that script (after you press the Start button). Can be an adding script so expensive?

您能告诉我为什么该脚本如此昂贵的CPU吗?我是javascript的新手,但我认为简单的算术运算会消耗过多的客户端CPU。可以吗?

Can you tell me why that script is so CPU expensive? I'm new to javascript but i think that simple arithmetic operations are consuming too much client CPU. Is that ok?

推荐答案

大量增加的数字是CPU使用率高的原因。在我的AMD Phenom II X2上,运行时它在Firefox上运行约19%的CPU。该页面的来源实际上是 http://www.realtimestatistics.org/ ,如果您查看javascript,它很模糊,但是我怀疑有些setTimeout()/ setInterval函数会在每个时间段内增加一定数量的数量。以目前的速度,我怀疑它可能在一秒钟内用完所有1000个插槽,或者至少要看到一百个插槽,才能看到数字的平滑程度。然后,它可能必须使用document.getElementById()函数来更新您的页面,并且数字甚至被格式化,并且是可爱的蓝色,这甚至增加了负载。

The sheer amount of numbers being increased are the source of the high CPU usage. On my AMD Phenom II X2, it runs up about 19% of CPU on Firefox when running it. The source of that page is actually http://www.realtimestatistics.org/ , and if you look at the javascript, it's obfuscated, but I suspect some setTimeout()/setInterval function that increases the number by an amount every period of time. At the rate that's going, I suspect it's probably using up all 1000 slots in a second to do that, or at least a hundred, seeing how smooth the numbers are. Then it has to update your page, probably using a document.getElementById() function, and the numbers are even formatted, and are a lovely blue, which adds even more to the load.

简单的算术运算确实会消耗CPU,但是像我的3.4 gHz双核这样的现代处理器却可以立即通过它们,并且您甚至看不到CPU使用率急剧上升。任务管理器。但是,来自BBC News的脚本上的CPU使用率几乎是恒定的,并且当您检查任务管理器时确实会显示出来。只要不需要花费很长时间即可执行,不会冻结/挂起页面或不会重复执行,算术就不会对客户端造成问题。

Simple arithmetic does consume CPU, but modern processors like my 3.4 gHz dual core can blast through them in no time, and you don't even see that CPU usage spike in Task Manager. However, the CPU usage on that script from BBC News is almost a constant stream, and that does show up when you check Task Manager. Arithmetic is no problem on clients, as long as it doesn't take long to execute, does not freeze/hang the page, or isn't recurring.

编辑:经过一些反编译和我以前的逆向工程时代的一些技巧之后,这是在该页面上运行的脚本文件的来源:
http://news.bbc.co.uk/nol/shared/spl/hi/sci_nat /10/the_internet/counters/js/internet_ticker.js

After a bit of decompiling and some eh, tricks from my old reverse engineering days, here's the source of the script file being ran on that page: http://news.bbc.co.uk/nol/shared/spl/hi/sci_nat/10/the_internet/counters/js/internet_ticker.js

如果您看一下,这是更新行:

If you look at it, this is the line for updating:

var currentValue = Math.floor(details.base + details.rate/1000 * differenceInMS(details.start, new Date()));
        glow.dom.get(details.div).html("<strong>"+commaFormatted(currentValue)+"</strong>");

添加,分割和铺地板,这都是非常费力的工作,甚至必须创建一个以及新的Date()变量。跟踪 differenceInMS()函数,您会看到更多的下限和时间函数。如果我这样做的话,我会用一个算术函数做一个简单的setTimeout(),并且可能花费更少的人力来保持当前的日期和时间。哦,在那里,您已经有了 glow.dom.get()必须搜索并找到页面的那一部分,然后在更新之前将数字转换为逗号形式。整个脚本非常渴求处理能力。

It's adding, then dividing, and flooring, all pretty labor intensive, and it even has to create a new Date() variable as well. And tracing that differenceInMS() function, you see that you get even more of flooring and time functions. If I was doing this, I would have just did a simple setTimeout() with arithmetic, and probably a bit less labor intensive to keep finding the current date and time. Oh, and there you have it glow.dom.get() has to search and find that part of the page, and then it converts the numbers into comma form, before updating. Entire script is pretty thirsty for processing power.

经验教训:
要么劳动强度大,时间短,要么简单而又长。不要尝试做他们正在做的事情,既要占用大量处理器,又要间隔很短的时间重复执行。这会迅速拖累您的CPU。如果在我的双重固化3.4 gHz AMD Phenom芯片上平均燃烧19%,请想象它可以在旧笔记本电脑上运行。这很容易使处理器最大负荷,并导致页面挂起和死机。哦,作为补充,在x64版本的Firefox上运行可降低x64处理器上的CPU使用率。我多年来恳求Adobe和Firefox使用x64的另一个原因。为Firefox 4.0努力奋斗。

Lesson learned: Either go with labor intensive and be short, or simple and long. Don't try to do what they're doing, both processor intensive and recurring at short time intervals. That drags down your CPU quickly. If that's burning 19% average on my dual cure 3.4 gHz AMD Phenom, imagine it running on something like an old laptop. That could easily max out the processor and cause page hangs and freezes. Oh, and as an added side note, running on the x64 version of Firefox is lowering the CPU usage on my x64 processor. Another reason for my years of begging Adobe and Firefox to go x64. Fingers crossed for Firefox 4.0.

这篇关于CPU昂贵的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆