JavaScript 加速?

发布于 2024-08-21 09:31:40 字数 49 浏览 3 评论 0原文

有什么方法可以加速 JS 脚本(我的意思是一些复杂的 DOM 操作,如游戏或动画)?

Is there any way to speed up JS scripts (i mean some complex DOM manipulations, like games or animations)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苏别ゝ 2024-08-28 09:31:40

确实没有办法真正加快速度。你可以压缩它,但它不会快得多。

Theres really no way to really speed it up. You can compact it but it won't be much faster.

貪欢 2024-08-28 09:31:40

使用V8 JavaScript 引擎? :P

唯一的方法是减少代码中 dom 和范围访问的数量。
例如当多次访问dom中的某个元素时,而不是

document.something.mydiv.myelement.property1 = this
document.something.mydiv.myelement.property2 = bla
document.something.mydiv.myelement.property3 = foo
document.something.mydiv.myelement.property4 = bar

do

var myel = document.something.mydiv.myelement
myel.property1 = this
myel.property2 = bla
myel.property4 = bar
myel.property3 = foo

注意,有些javascript引擎会自动为你做这个优化,但是对于老的浏览器来说,后者肯定会比前者更快,因为它只需要经过访问链接一次即可到达 myel

观看此视频以了解更多 javascript 优化技术
http://www.youtube.com/watch?v=mHtdZgou0qU

Use the V8 javascript engine? :P

The only way you can do that is to reduce the amount of dom and scope access in your code.
e.g. when accessing an element in the dom multiple times, instead of

document.something.mydiv.myelement.property1 = this
document.something.mydiv.myelement.property2 = bla
document.something.mydiv.myelement.property3 = foo
document.something.mydiv.myelement.property4 = bar

do

var myel = document.something.mydiv.myelement
myel.property1 = this
myel.property2 = bla
myel.property4 = bar
myel.property3 = foo

Note that some javascript engines will automatically make this optimization for you, but for older browsers, the latter will definitely be faster than the former because it only has to go through the access chain once to reach myel

Have a look at this video for more javascript optmization techniques
http://www.youtube.com/watch?v=mHtdZgou0qU

葬心 2024-08-28 09:31:40

如果您指的是浏览器之外,那么您应该使用最快的,即 Chrome 的 V8 Java 脚本引擎

浏览器内部有大量的优化技术可以更快地加载 Java 脚本,这里是优化的好地方谷歌的技术

  • 使用 YUI 压缩器等工具编译 Java 脚本,然后将其压缩。
  • 仅加载所需的最低限度
  • 复杂的动画仍然最好由丰富的 UI 插件提供,即 Flash/Silverlight

对于动画,请在支持它的浏览器中使用 HTML 5 Canvas 元素,对于不支持的浏览器则回退到 flash。

Google 地图是纯 Java 脚本的一个很好的例子,尽管他们花费了大量资源来优化每个浏览器的性能。与往常一样,提高速度的最佳方法是对不同的方法进行基准测试。例如 div.innerHTML ="",大多数时候比使用 DOM 动态添加元素等更快。

If you mean outside the browser then you should use the fastest around, i.e. Chrome's V8 Java Script engine.

Inside the browser there is a wealth of optimization techniques to faster loading Java Script, here is a good place for optimization techniques by google.

  • Compile your Java Script using a tool like YUI compressor than serve it gzipped.
  • Only load the bare minimum you need
  • Complex animations are still best served by Rich UI plugins, i.e. Flash/Silverlight

For animations look at using the HTML 5 Canvas element for browsers that support it, fall back to flash for ones that don't.

Google maps is a good case of what's possible with pure Java Script although they've spent a wealth resources optimizing the performance for each browser. As always the best way to improve your speed is to benchmark different approaches. e.g. div.innerHTML ="", is most of the times quicker than using the DOM to dynamically add elements, etc.

止于盛夏 2024-08-28 09:31:40

您能做的最好的事情就是优化您的代码。使用 分析器 - 对于 Firefox,有 Firebug,Safari 和 Windows IE 8 有 JavaScript内置调试器和分析器(至少我相信 IE 8 是这样,如果我错了,有人会纠正我......)。在代码上运行配置文件将向您显示最慢的部分在哪里,这些部分是您可以重点优化的部分......也许还有更多更具体的问题。

The best you can do is optimize your code. Use a profiler -- for Firefox there's Firebug, Safari and Windows IE 8 have JavaScript debuggers and profilers built in (At least I believe IE 8 does, someone correct me if I'm wrong...). Running a profile on your code will show you where the slowest parts are, and those are the sections you can focus on optimizing... perhaps with more questions that are a lot more specific.

喵星人汪星人 2024-08-28 09:31:40

这是一个非常模糊的问题。您可以采取一百万种方法来加快代码速度(Ajaxian157 篇关于该主题的文章(在撰写本文时),但没有“让我更快”按钮神奇地使所有脚本运行快点。如果有的话,生活就会容易得多。

That's a very vague question. There are a million things you can do to speed up your code (Ajaxian has 157 articles on the topic at the time of this writing), but there is no "Make Me Faster" button that magically makes all scripts run faster. If there were, life would be so much easier.

烂人 2024-08-28 09:31:40

Google 的关闭项目提出了一些类似的主张,尽管我还没有亲自尝试过。

闭包编译器将 JavaScript 编译成紧凑的高性能代码。编译器会删除无效代码并重写并最小化剩余代码,以便快速下载和运行。它还检查语法、变量引用和类型,并对常见的 JavaScript 陷阱发出警告。

The closure project from Google makes some claims along those lines, although I haven't tried it personally.

The Closure Compiler compiles JavaScript into compact, high-performance code. The compiler removes dead code and rewrites and minimizes what's left so that it downloads and runs quickly. It also also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.

日裸衫吸 2024-08-28 09:31:40

尝试制作动画并显示对定位或“屏幕外”元素的更改 - 以最少的次数重绘页面。

通过更改 cssText 或 className 来更改多种样式,而不是一次更改一个属性。

如果您需要在同一进程中两次查找某个元素或属性,则应该在第一次时进行本地引用。

如果不进行调试,请记住关闭调试器。

Try to make animation and display changes to positioned or 'offscreen' elements- redraw the page the fewest number of times.

Make multiple style changes by changing the cssText or the className, not one property at a time.

If you need to lookup an element or property twice in the same process, you should have made a local reference the first time.

And remember to turn off the debugger, if you are not debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文