內存分配對性能的影響是很大的,分配內存本身需要時間,垃圾回收器回收內存也需要時間,所以應該盡量避免在堆里分配內存。不過直到最近優化HoLa cantk時,我才深刻的體會到內存分配對性能的影響,其中有一個關于arguments的問題挺有意思,寫在這里和大家分享一下。
我要做的事情是用webgl實現canvas的2d API(這個話題本身也是挺有意思的,有空我們再討論),drawImage是一個重要的函數,游戲會頻繁的調用它,所以它的性能至關重要。drawImage的參數個數是可變的,它有三種形式:
第一個版本大概是這樣實現的:
function Context() { } Context.prototype.drawImage3 = function(image, x, y) { this.drawImage9(image, 0, 0, image.width, image.height, x, y, image.width, image.height); } Context.prototype.drawImage5 = function(image, dx, dy, dw, dh) { this.drawImage9(image, 0, 0, image.width, image.height, dx, dy, dw, dh); } Context.prototype.drawImage9 = function(image, sx, sy, sw, sh, dx, dy, dw, dh) { //DO IT } Context.prototype.drawImage = function(image, a, b, c, d, e, f, g, h) { var n = arguments.length; if(n === 3) { this.drawImage3(image, a, b); }else if(n === 5) { this.drawImage5(image, a, b, c, d); }else if(n === 9) { this.drawImage9(image, a, b, c, d, e, f, g, h); } }
本文題目:動態內存分配導致影響Javascript性能的問題-創新互聯
本文網址:http://m.newbst.com/article36/dehspg.html
成都網站建設公司_創新互聯,為您提供軟件開發、建站公司、用戶體驗、網站建設、營銷型網站建設、商城網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯