主頁 > 知識庫 > 利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本

利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本

熱門標(biāo)簽:宿遷智能外呼系統(tǒng)排名 400電話辦理信任翰諾科技 廣州銷售外呼系統(tǒng)定制 福州人工智能電銷機器人加盟 電銷機器人 數(shù)據(jù) ai電銷機器人對貸款有幫助嗎 云狐人工智能電話機器人 地圖標(biāo)注多少錢一張 怎樣給陜西地圖標(biāo)注顏色

界面如下:

然后,click filter button后的UI:

看,篩選出來了,代碼如下(js能調(diào)C#寫的UserInfo類的各個屬性)

復(fù)制代碼 代碼如下:

public class UserInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return string.Format("{0}, {1}: {2}", FirstName, LastName, Age);
        }
    }

private void btnSearch_Click(object sender, EventArgs e)
        {
            ListUserInfo> users = FilterUsers(txtFilter.Text.Trim());
            lstUsers.DataSource = users;
        }

        private ListUserInfo> FilterUsers(string jsFilter)
        {
            ListUserInfo> results = new ListUserInfo>();
            if (jsFilter.Trim().Length == 0)
                return this.users;

            try
            {
                using (JavascriptContext context = new JavascriptContext())
                {
                    foreach (UserInfo user in this.users)
                    {
                        context.SetParameter("user", user);
                        context.SetParameter("isInclude", false);
                        string js = string.Format(@"
                                                if({0})
                                                    isInclude=true;
                                                else
                                                    isInclude=false;", jsFilter);
                        context.Run(js);
                        bool isInclude = (bool)context.GetParameter("isInclude");
                        if (isInclude)
                            results.Add(user);
                    }
                }
            }
            catch
            {
                txtFilter.Focus();
                txtFilter.SelectAll();
                return this.users;
            }

            return results;
        }

我們來看看效率:

復(fù)制代碼 代碼如下:

private void btnTest_Click(object sender, EventArgs e)
        {
            using (JavascriptContext context = new JavascriptContext())
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                int i=1000000;
                while(i-- > 0)
                {
                    context.SetParameter("ii", 1);
                    context.SetParameter("jj", 2);
                    context.SetParameter("sum", 0);
                    string js = string.Format(@"sum=ii+jj;");
                    context.Run(js);
                    int sum = (int)context.GetParameter("sum");
                }
                sw.Stop();

                long useJS_Total = sw.ElapsedMilliseconds;
                float useJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                sw.Reset();
                sw.Start();
                i = 1000000;
                while (i-- > 0)
                {
                    int ii = 1;
                    int jj = 2;
                    int sum = 0;
                    sum = ii + jj;
                }
                sw.Stop();

                long nouseJS_Total = sw.ElapsedMilliseconds;
                float nouseJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                string msg = "";
                msg += string.Format("useJS_Total: {0}\r\n", useJS_Total);
                msg += string.Format("useJS_Average: {0}\r\n\r\n\r\n", useJS_Average);
                msg += string.Format("nouseJS_Total: {0}\r\n", nouseJS_Total);
                msg += string.Format("nouseJS_Average: {0}\r\n\r\n\r\n", nouseJS_Average);

                MessageBox.Show(msg);
            }
        }

結(jié)果(上面的是通過js引擎計算的時間、下面的是純C#計算時間,悲劇?。哼@個Noesis.Javascript只適用于性能要求不高的場合

您可能感興趣的文章:
  • 詳解Chart.js輕量級圖表庫的使用經(jīng)驗
  • 常用的9個JavaScript圖表庫詳解
  • 使用Chart.js圖表庫制作漂亮的響應(yīng)式表單
  • JavaScript可視化圖表庫D3.js API中文參考
  • javaScript矢量圖表庫-gRaphael幾行代碼實現(xiàn)精美的條形圖/餅圖/點圖/曲線圖
  • Node.js開源應(yīng)用框架HapiJS介紹
  • 開源的javascript項目Kissy介紹
  • Java中使用開源庫JSoup解析HTML文件實例
  • jQuery EasyUI 開源插件套裝 完全替代ExtJS
  • 優(yōu)秀js開源框架-jQuery使用手冊(1)
  • 一個加密JavaScript的開源工具PACKER2.0.2
  • 4個頂級開源JavaScript圖表庫

標(biāo)簽:新疆 延安 曲靖 綿陽 宜春 大興安嶺 黃南 焦作

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本》,本文關(guān)鍵詞  利用,noesis.Javascript,開源,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章