优化代码...... [英] optimizing code...

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

问题描述

我正在编写一个大型的Javascript应用程序(未压缩的源代码,价值为
400K),它正在进行几乎所有初始化,它只需要在
中进行初始化。时间的方式。但是,我已经为应用程序首次启动时提供了几乎所有

的选项。


在这种情况下,从最后几个开始每晚构建

Mozilla,我一直在:

脚本警告:


此页面上的脚本导致mozilla跑得慢。如果它继续运行,你的计算机可能会没有响应。


你想中止脚本吗?

我想这是显示的,因为它需要花费时间来开始上涨而不是它所涉及的代码量,但是它是那么好

是任何看待优化代码的借口。 [我刚刚意识到并且

修复了一个处理器密集的代码块,正在处理中,在启动期间已经处理了6次
;除了前四个(可能是五个,取决于具体情况,价格为b $ b)之外完全没必要!]


我需要的是:

1.一种能够强行跳过此消息的方法


2.一种比(x列表中)更快的循环形式

3.比SELECT列表的快速排序更快的排序


4.更快的搜索形式 - 我正在使用二进制搜索


5.在SELECT列表中的任何一点获取一个OPTION的快速方法是将

移到列表顶部。我现在正在使用一种类似于冒泡的方法。

[列表最多可达1000件......]


我知道我可能在棍子上要求月亮,但是任何帮助都会很高兴。


谢谢,


Ian

解决方案

Ian Richardson于2004年2月16日在comp.lang.javascript写了

我正在编写一个大型Javascript应用程序(围绕
400K的未压缩源代码),它正在以及时的方式完成它所需的几乎所有初始化。但是,我已经为应用程序首次启动时提供了几乎所有这一切的选项。

在这种情况下,以及自从最后几个每晚构建的Mozilla ,我一直在收到:

脚本警告:

此页面上的脚本导致mozilla运行缓慢。如果它继续运行,你的计算机可能会没有响应。

你想中止脚本吗?
[....]




脚本引擎,即解释语言模拟器,也称为

解释器,不能快速执行。为什么不用一个将被编译的

语言的程序?然后设置时间结束动态

执行时间会好几个数量级。


如果压缩javascript脚本的源代码,设置时间

会更长,我想。


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)


Evertjan。写道:

Ian Richardson于2004年2月16日在comp.lang.javascript写了

我''我正在编写一个大型Javascript应用程序(未压缩的源代码400K),它正在以及时的方式进行几乎所有初始化。但是,我已经为应用程序首次启动时提供了几乎所有这一切的选项。

在这种情况下,以及自从最后几个每晚构建的Mozilla ,我一直在收到:

脚本警告:

此页面上的脚本导致mozilla运行缓慢。如果继续运行,你的计算机可能会没有响应。

你想中止脚本吗?
[....]

脚本引擎,即解释语言模拟器,也称为
解释器,不能快速执行。为什么不用一种将要编译的语言编写程序?然后设置时间结束,动态执行时间会好几个数量级。




唯一真正的选择是用Java重写整个事情。 。我是

不期待那个!!我必须首先学习Java ...

如果压缩javascript脚本的源代码,设置时间
会更长,我想。




取出所有多余的评论和空白实际上确实节省了一些时间,但没有什么比这更好了。这就是为什么我要寻找

尽可能优化代码。


Ian


" Ian Richardson" < ZA ***** @ chaos.org.uk>在留言中写道

news:c0 ************* @ ID-99375.news.uni-berlin.de ...

< snip>

我需要的是:
1。能够强制跳过此消息的方法


没有从javascript禁用消息的机制。所以

远远可以防止它通常通过将代码分成

分成块并按顺序执行每个setTimeout来完成,所以

浏览器有机会赶上它需要做的事情,并重置

它用来衡量脚本运行时间的计数器。

2。一个更快的循环形式比(列表中的x)


所有的for-for循环选项 - 对于(obj中的var prop) - 肯定是

最慢,但没有人知道是否有更好的选择(甚至是适用的b $ b),而不知道其使用的背景。

3.比快速排序更快的排序一个SELECT列表


尝试对select元素中的

选项进行操作的排序与它使用的数据结构排序之间存在很大差异

建立一个选项列表。关于这个问题,可以说任何有用的东西是不可能的,因为能够看到实现。

4.更快的搜索形式 - 我正在使用二元搜索


二元搜索,正确实现,速度难以击败。

5.快速获取一个OPTION的方法SELECT列表
移到列表顶部。我正在使用类似于冒泡的方法。


将项目移到列表顶部有点奇怪

要求考虑/移动两个项目(一个项目是转到

列表的顶部和已存在的那个)。这意味着

在任务规范中还有其他标准。

[列表最多可以包含1000个项目...]


一千件物品并不是那么糟糕,你不能期望从浏览器/ javascript组合中获得瞬间

结果但是通常有空间

用于改进。

我知道我可能会在棍子上要求月球,但是任何帮助都会非常感激。




随着javascript的性能往往是一个折衷,通常交换增加内存使用的性能。一般来说,你应该在任何可行的情况下避免

多次做同样的事情。除了形式那个

还有很多小东西可以用一种方式来抑制

性能累积在一个大脚本中,但很难

通过讨论实际代码来概括和最好地解决。在优化实施中,这个群体也有很多兴趣。


不幸的是,400Kb的帖子可能不受欢迎,是一个

示例页面(带有脚本)会更好,但是你可能不会觉得

将你的作品公之于众。但是你没有得到很多帮助

没有显示一些相关的代码(及其

上下文)。


Richard。


I''m writing a large Javascript application (uncompressed source around
400K) which is doing almost all the initialisation it needs to in a
just-in-time manner. However, I have included an option for almost all
of this to be done when the application first starts.

Under this circumstance, and since the last few nightly builds of
Mozilla, I''ve been getting:
Script warning:

A script on this page is causing mozilla to run slowly. If it continues
to run, your computer may become unresponsive.

Do you want to abort the script?
I think this is being displayed because of the time it''s taking to start
up rather than the amount of code it''s wading through, but it''s as good
an excuse as any to look at optimizing code. [I''ve just realised and
fixed one processor-intensive chunk of code which was being processed
six times during startup; fine except the first four (possibly five,
depending on circumstances) were completely unnecessary!]

What I need is:

1. A means of being able to forcibly skip this message

2. A faster form of loop than for(x in list)

3. A faster sort than a quicksort for a SELECT list

4. A faster form of search - I''m currently using binary search

5. A fast way of getting one OPTION at any point in a SELECT list moved
to the top of the list. I''m currently using a method akin to a bubble sort.
[Lists can be up to 1000 items...]

I know I might be asking for the moon on a stick, but any help would be
greatly appreciated.

Thanks,

Ian

解决方案

Ian Richardson wrote on 16 feb 2004 in comp.lang.javascript:

I''m writing a large Javascript application (uncompressed source around
400K) which is doing almost all the initialisation it needs to in a
just-in-time manner. However, I have included an option for almost all
of this to be done when the application first starts.

Under this circumstance, and since the last few nightly builds of
Mozilla, I''ve been getting:
Script warning:

A script on this page is causing mozilla to run slowly. If it continues
to run, your computer may become unresponsive.

Do you want to abort the script?
[....]



Script engines, that is interpreted language emulators, also called
interpreters, are not up to fast execution. Why not make a programme in a
language that will be compiled? Then the setup time end the dynamic
execution time will be orders of magnitude better.

If you compress the source code of your javascript script, the setup time
will be even longer, I suppose.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


Evertjan. wrote:

Ian Richardson wrote on 16 feb 2004 in comp.lang.javascript:

I''m writing a large Javascript application (uncompressed source around
400K) which is doing almost all the initialisation it needs to in a
just-in-time manner. However, I have included an option for almost all
of this to be done when the application first starts.

Under this circumstance, and since the last few nightly builds of
Mozilla, I''ve been getting:
Script warning:

A script on this page is causing mozilla to run slowly. If it continues
to run, your computer may become unresponsive.

Do you want to abort the script?
[....]

Script engines, that is interpreted language emulators, also called
interpreters, are not up to fast execution. Why not make a programme in a
language that will be compiled? Then the setup time end the dynamic
execution time will be orders of magnitude better.



The only real option is to rewrite the whole thing in Java... and I''m
not looking forward to that!! I''d have to learn Java, first...
If you compress the source code of your javascript script, the setup time
will be even longer, I suppose.



Taking out all the redundant comments and whitespace does actually save
a bit of time, but nothing like enough. That''s why I''m looking to
optimize the code as much as possible.

Ian


"Ian Richardson" <za*****@chaos.org.uk> wrote in message
news:c0*************@ID-99375.news.uni-berlin.de...
<snip>

What I need is:
1. A means of being able to forcibly skip this message
The are no mechanisms for disabling the message from javascript. In so
far as it can be prevented it is usually done by splitting the code up
into chunks and executing each in sequence with setTimeout so the
browser gets a chance to catch up with what it needs to do, and reset
the counters it uses to measure the time a script has been running.
2. A faster form of loop than for(x in list)
Of all of the - for - loop options - for(var prop in obj) - is certainly
slowest but nobody can tell if any alternatives would be better (even
applicable) without knowing the context of its use.
3. A faster sort than a quicksort for a SELECT list
There is a big difference between a sort that tries to operate on the
options in a select element and sorting a data structure that it used to
build an options list. It is unlikely that anything useful can be said
on the subject witho9ut being able to see the implementation.
4. A faster form of search - I''m currently using binary search
A binary search, properly implemented, is hard to beat for speed.
5. A fast way of getting one OPTION at any point in a SELECT list
moved to the top of the list. I''m currently using a method akin
to a bubble sort.
That is a bit strange as moving an item to the top of a list only
requires that two items are considered/moved (the one that is to go to
the top of the list and the one already there). That would imply that
there are other criteria in the specification for the task.
[Lists can be up to 1000 items...]
One thousand items is not that bad, you can''t expect instantaneous
results form the browser/javascript combination but there is often room
for improvement.
I know I might be asking for the moon on a stick, but any help
would be greatly appreciated.



With javascript performance is often a trade off, usually exchanging
increased memory use for performance. Generally you should aim to avoid
doing the same thing more than once wherever practical. Apart form that
there are lots of little things that done one way can inhibit
performance when accumulated in a big script, but are difficult to
generalise about and best addressed by discussing actual code. There is
also a lot of interest on this group in optimum implementations.

Unfortunately a 400Kb post would probably be unwelcome, a URL to an
example page (with scripts) would be better, but you may not feel like
rendering your work to date public. But you will not get much help with
the specifics without showing at leas some of the relevant code (and its
context).

Richard.


这篇关于优化代码......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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