Using Noesis. JavaScript open Source component. NET to execute JavaScript script _ Practical Tips

Source: Internet
Author: User

The interface is as follows:

Then, click the UI after the filter button:

Look, the filter out, the code is as follows (JS can tune C # write the UserInfo class of the various attributes)

Copy Code code as follows:

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)
{
list<userinfo> users = Filterusers (TxtFilter.Text.Trim ());
Lstusers.datasource = users;
}

Private list<userinfo> Filterusers (string jsfilter)
{
list<userinfo> results = new list<userinfo> ();
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;
}

Let's take a look at efficiency:

Copy Code code as follows:

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);
}
}

Results (above is calculated by the JS engine time, the following is pure C # calculation time, tragedy AH): This noesis.javascript only apply to the performance requirements of the occasion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.