国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

探討一種AJAX性能的改進方法

2024-07-21 02:28:49
字體:
來源:轉載
供稿:網友

  一、 引言

  在web表單中,我們使用ajax來從客戶端(通過javascript)調用服務端方法,而在ajax內部則進行xmlhttprequest調用。我測試了一些以不同方式實現的ajax函數。另外,我還監控分析了進行ajax調用的性能和生命周期。結果,我發現在web表單中使用ajax時存在一些嚴重的問題。不過,我也找到了這些問題的一種解決方法。在本文中,我正是想與各位分析這一問題及其相應的解決方案。

  二、 在使用ajax時所遇到的性能問題

  對于每一個ajax調用來說,我們都要創建包含ajax方法的類的一個實例。另外,如果我們在類級上使用new關鍵字的話,我們還要為字段、屬性及其它類級的變量創建實例。

  三、 實現方案

  我創建了一個工程,它包含兩個web表單:webform1.aspx和webform2.aspx,還有一個類student.vb。這兩部分code-behind頁面都使用了一個ajax函數getdata()和一個student類型的公共變量。借助于mxlogger類,我記錄下每一個階段的執行流程。

  注意:webform2.aspx的ajax函數getdata()是共享的,而在webform1中,它不是共享的。

'student.vb
public class student
 sub new()
  mxlogger.addlog("from student.constructor")
 end sub
 dim _name as string
 public property name() as string
  get
   return _name
  end get
  set(byval value as string)
   _name = value
  end set
 end property
end class
'webform1.aspx.vb
public class webform1
public student as new student
sub new()
 mxlogger.addlog("from webform1.constructor")
end sub
<ajax.ajaxmethod(ajax.httpsessionstaterequirement.read)> _
public function getdata() as string
 mxlogger.addlog("from webform1.ajax.getdata()")
 return "i m a non shared function"
end function
end class
'webform2.aspx.vb
public class webform2
public student as new student
sub new()
 mxlogger.addlog("from webform2.constructor")
end sub
<ajax.ajaxmethod(ajax.httpsessionstaterequirement.read)> _
public shared function getdata() as string
 mxlogger.addlog("from webform2.ajax.getdata()")
 return "i m a shared function"
end function
end class

  四、 測試應用程序

  · 測試用例1:

  運行webform1.aspx并且從javascript中調用getdata() ajax函數三次。

  · 測試用例2:

  運行webform2.aspx并且從javascript中調用getdata()ajax函數三次。

  對于上面的測試用例,我得到如下的日志輸出數據:

//請注意,為了解釋之目的,我在其中手工加入了一些日志行
log for the test case 1: ( non ajax shared function )
-------while loading the page--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------first call for getdata()--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------second call for getdata()--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------third call for getdata()--------
5/9/2006 10:37:30 am>>from student.constructor
5/9/2006 10:37:30 am>>from webform1.constructor
5/9/2006 10:37:30 am>>from webform1.ajax.getdata()
log for the test case 2: ( shared ajax function )
-------while loading the page--------
5/9/2006 10:37:09 am>>from student.constructor
5/9/2006 10:37:09 am>>from webform2.constructor
5/9/2006 10:37:09 am>>from webform2.ajax.getdata()
-------first call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()
-------second call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()
-------third call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()

  我們可以看到,在上面的日志輸出數據中,對于測試用例1來說,我們能夠看到更多的來自于webform1和student的構造器的日志數據。

  五、 結論

  我的建議是,在所有可能的地方,我們應該使用針對于ajax的共享方法,以便它不會創建更多的web表單實例和類級的字段。這樣以來,我們就可以減少從gc中調用finalize()的次數。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉祥县| 鹤壁市| 靖安县| 盐池县| 宜城市| 肥乡县| 承德市| 定安县| 周口市| 堆龙德庆县| 北京市| 丰宁| 马山县| 闻喜县| 石台县| 江西省| 科尔| 廉江市| 麦盖提县| 师宗县| 繁峙县| 鄢陵县| 兴义市| 鄂伦春自治旗| 扎鲁特旗| 延津县| 泸定县| 清苑县| 建昌县| 运城市| 沁水县| 崇州市| 贺兰县| 张家港市| 杨浦区| 南投县| 蒙山县| 和硕县| 神池县| 十堰市| 翁牛特旗|