How do i separate JavaScript View/Logic code properly

I receive JSON objects from a websocket with update/create/delete flags. Based on this information, I either update, create or delete HTML elements and bind callbacks. This can affect multiple HTML elements. My current approach was to put everything into specific objects which handle HTML generation via jquery eg: $.("<table>").addChild($("<tr>")).addClass('test') and bind event

如何正确分离JavaScript视图/逻辑代码

我使用update / create / delete标志从websocket接收JSON对象。 基于这些信息,我可以更新,创建或删除HTML元素并绑定回调。 这可能会影响多个HTML元素。 我目前的做法是把所有东西都放到通过jquery处理HTML代码的特定对象中,例如: $.("<table>").addChild($("<tr>")).addClass('test') 并绑定事件监听器。 但随着越来越多的代码的增加,它变得非常混乱,现在我正在寻找合适的方法来分离代码。 有没有关

Why does this function run faster with long numbers than short numbers?

I wrote a JS perf to show that parsing the digits in a number via magnitude reduction is faster than by string evaluation (for a sorting task). What I found though is that the magnitude reduction algorithm performs operations faster for longer numbers (not per digit, but in total), even though it should loop more times for those longer numbers. It happens in Chrome, Safari and Firefox on OS X (

为什么这个函数在长数字比短数字更快运行?

我写了一个JS perf来表明通过减少量级来解析数字中的数字比字符串评估(对于排序任务)更快。 我发现虽然减号算法对更长的数字执行操作的速度更快(不是每个数字,但是总数),即使它应该为更长的数字循环更多次。 它发生在OS X上的Chrome,Safari和Firefox上(尚未测试其他操作系统)。 任何想法这里发生了什么? 以下是perf:http://jsperf.com/get-digits-from-integer 编辑这是一个表现不同数字的8,16,32,64和128数

Can Javascript Prototype Pattern Performance Be Improved

I have read that creation of objects via prototypes is faster and uses less memory than closures because closures need to re-create all the functions. Then accessing a function that is on the object's prototype is a little slower because it needs to traverse the prototype chain further to find the function. I am wondering if adding a reference in the constructor to the prototype function c

可以改进Javascript原型模式的性能

我已经读过通过原型创建对象的速度更快,并且使用的内存少于闭包,因为闭包需要重新创建所有功能。 然后,访问该对象原型上的函数会稍微慢一点,因为它需要进一步遍历原型链来查找函数。 我想知道如果在构造函数中添加一个引用到原型函数可以提高性能,只需极小的内存和初始安装影响。 这可以通过这样的事情来实现...... function Customer2(name) { this.name = name; this.greet = this.constructor.prototype.gre

How does a constructor that just returns an object literal work?

Both of the following constructor functions work the same (as far as I have been able to tell) when I invoke them as: var x2z = new xyz('ralph'); whether I write a constructor function like this: function xyz(name) { return { name: name, x:1, get y() {return this.x+1}, get z() {return this.y+1} } } or if I had declared it: function xyz(name) { this

一个刚刚返回对象文字的构造函数是如何工作的?

当我将它们调用为以下时,以下两个构造函数的功能都相同(据我所知): var x2z = new xyz('ralph'); 是否我写这样的构造函数: function xyz(name) { return { name: name, x:1, get y() {return this.x+1}, get z() {return this.y+1} } } 或者如果我宣布它: function xyz(name) { this.name = name; this.x = 1; Object.defineProperties(this, { "y": {"g

Javascript prototype vs $.extend

Related Questions, but no mention of my specific 'bug' - prototypes vs extending objects Advantages of using prototype, vs defining methods straight in the constructor? I have a performance issue I don't understand. I was creating a class with the ability to be both callable and have methods (similar to jQuery). I allowed an 'opt out' of the callable part, assuming th

Javascript原型vs $ .extend

相关的问题,但没有提到我的具体'错误' - 原型vs扩展对象 使用原型的优点,vs直接在构造函数中定义方法? 我有一个我不明白的表现问题。 我创建了一个类,可以调用并具有方法(类似于jQuery)。 我允许“可选择”可调用部分,假定用方法扩展函数比使用原型要慢。 这个假设在最基本的情况下是正确的。 但是,当我真正计算代码的时间并查看内存使用情况时,我惊讶地发现原型方法速度较慢,占用内存较多。 仔

JS: Why Is This Slower? It Shouldn't Test Other OR Conditions But It Does?

I just tested something. I always thought that in a OR condition, once the computer/browser finds something true, it returns it and doesn't test the other conditions. I structured my code around this assumption. However, I timed it and it appears that the long test takes x4 more times, any explanation for that? Note: Tested in Google Chrome console. JSPerf: http://jsperf.com/or-cond

JS:为什么这会更慢? 它不应该测试其他或条件,但它呢?

我只是测试一些东西。 我一直认为,在一个OR条件下,一旦计算机/浏览器发现一些真实的东西,它会返回它并且不会测试其他条件。 我围绕这个假设构建了我的代码。 但是,我计算了它,似乎长时间的测试需要x4次,对此有任何解释? 注意:在Google Chrome控制台中进行测试。 JSPerf: http://jsperf.com/or-condition return true || 1 return true || 1 http://jsperf.com/or-condition2 var condition = true || 1; re

Javascript performance of Array.map

Just wrote up some test cases in jsperf to test the difference between named and anonymous functions while using Array.map and other alternatives. http://jsperf.com/map-reduce-named-functions (excuse the url name, there is no testing of Array.reduce in here, I named the test before fully deciding on what I wanted to test) A simple for/while loop is obviously the fastest, I'm still surpr

Array.map的Javascript性能

刚刚在jsperf中写了一些测试用例来测试命名函数和匿名函数之间的区别,同时使用Array.map和其他替代方法。 http://jsperf.com/map-reduce-named-functions (请原谅url名称,这里没有Array.reduce的测试,我在完全决定我想测试的内容之前命名测试) 一个简单的for / while循环显然是最快的,但我仍然感到惊讶,速度比Array.map慢10倍Array.map ...... 然后我通过mozilla尝试了polyfill https://developer.mozilla.org/en

Performance penalty for undefined arguments

I quite often have optional arguments in functions, but some testing is showing a huge performance hit for them in firefox and safari (70-95%). Strangely, if I pass in the literal value undefined then there is no penalty. What could be happening here? I wouldn't have thought that it was a scope chain issue as they are inherently local to the function. Am I to start passing undefined into

未定义参数的性能损失

我经常在函数中使用可选的参数,但是一些测试显示他们在firefox和safari(70-95%)中有巨大的性能影响。 奇怪的是,如果我传递未定义的字面值,那么就没有惩罚。 这里会发生什么? 我不会认为这是一个范围链问题,因为它们本身就是本地功能。 我是否会将undefined传入每个可选参数? jsPerf:http://jsperf.com/function-undefined-args/2 对于这样的功能: function threeArgs(x, y, z) { return x + y + z; } 这

Accessing local variable doesn't improve performance

****Clarification**: I'm not looking for the fastest code or optimization. I would like to understand why some code that seem to not be optimized or optimal run in fact in general consistently faster. The short version Why is this code: var index = (Math.floor(y / scale) * img.width + Math.floor(x / scale)) * 4; More performant than this one? var index = Math.floor(ref_index) * 4; Th

访问本地变量不会提高性能

****澄清**:我不是在寻找最快的代码或优化。 我想了解为什么一些似乎没有被优化或优化的代码实际上总体上运行得更快。 短版 为什么这个代码: var index = (Math.floor(y / scale) * img.width + Math.floor(x / scale)) * 4; 比这个更高性能? var index = Math.floor(ref_index) * 4; 长版 本周,Impact js的作者发表了一篇关于渲染问题的文章: http://www.phoboslab.org/log/2012/09/drawing-pixels-is-hard

for loop vs forEach performance in javascript and credibility of jsperf results

I don't trust results from jsperf measuring performance of for loop vs forEach. At least for chrome and firefox on my machine results are completely different than the ones being advertised in jsperf. http://jsperf.com/foreach-vs-loop (mine) http://jsben.ch/#/BQhED (more popular) On my laptop running Ubuntu 11.10 I have the following results in Firefox: for: total=1641 ms, avg=164.1 m

for循环与forEach性能在JavaScript和jsperf结果的可信度

我不相信jsperf衡量循环vs forEach性能的结果。 至少我的机器上的chrome和firefox的结果与jsperf中公布的完全不同。 http://jsperf.com/foreach-vs-loop(我的) http://jsben.ch/#/BQhED(更流行) 在运行Ubuntu 11.10的笔记本电脑上,我在Firefox中获得了以下结果: for: total=1641 ms, avg=164.1 ms forEach: total=339 ms, avg=33.9 ms uname -a: Linux 3.0.0-16-generic #29-Ubuntu SMP Tue Feb 14 12:48:51