Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
require_from_group2
(version: 3)
Performance testing for jquery-validate's require_from_group method
Comparing performance of:
built-in require_from_group vs modified require_from_group
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<form id="theform"> <input type="checkbox" name="a"/> <input type="checkbox" name="b"/> <input type="checkbox" name="c"/> <input type="checkbox" name="d"/> <input type="checkbox" name="e"/> <input type="checkbox" name="f"/> <input type="checkbox" name="g"/> <input type="checkbox" name="h"/> <input type="checkbox" name="i"/> <input type="checkbox" name="j"/> <input type="checkbox" name="k"/> <input type="checkbox" name="l"/> <input type="checkbox" name="m"/> <input type="checkbox" name="n"/> <input type="checkbox" name="o"/> <input type="checkbox" name="p"/> <input type="checkbox" name="q"/> <input type="checkbox" name="r"/> <input type="checkbox" name="s"/> <input type="checkbox" name="t"/> <input type="checkbox" name="u"/> <input type="checkbox" name="v"/> <input type="checkbox" name="w"/> <input type="checkbox" name="x"/> <input type="checkbox" name="y"/> <input type="checkbox" name="z"/> </form> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
Script Preparation code:
var validator = $('#theform').validate({ groups: { checkgroup: 'a b c d e f g h i j k l m n o p q r s t u v w x y z', }, }); $.validator.addMethod( "require_from_group_builtin", function( value, element, options ) { var $fields = $( options[ 1 ], element.form ), $fieldsFirst = $fields.eq( 0 ), validator = $fieldsFirst.data( "valid_req_grp" ) ? $fieldsFirst.data( "valid_req_grp" ) : $.extend( {}, this ), isValid = $fields.filter( function() { return validator.elementValue( this ); } ).length >= options[ 0 ]; // Store the cloned validator for future validation $fieldsFirst.data( "valid_req_grp", validator ); // If element isn't being validated, run each require_from_group field's validation rules if ( !$( element ).data( "being_validated" ) ) { $fields.data( "being_validated", true ); $fields.each( function() { validator.element(this); } ); $fields.data( "being_validated", false ); } return isValid; }, $.validator.format( "Please fill at least {0} of these fields." ) ); $.validator.addMethod( "require_from_group_modified", function( value, element, options ) { var $fields = $( options[ 1 ], element.form ), $fieldsFirst = $fields.eq( 0 ), validator = $fieldsFirst.data( "valid_req_grp" ) ? $fieldsFirst.data( "valid_req_grp" ) : $.extend( {}, this ), isValid = $fields.filter( function() { return validator.elementValue( this ); } ).length >= options[ 0 ]; // Store the cloned validator for future validation $fieldsFirst.data( "valid_req_grp", validator ); // If element isn't being validated, run each require_from_group field's validation rules if ( !$( element ).data( "being_validated" ) ) { $fields.data( "being_validated", true ); $fields.each( function() { /* Patch adapted from * https://www.drupal.org/project/clientside_validation/issues/2884667 * https://www.drupal.org/files/issues/clientside_validation-select_or_other_optimization-2884667-4-7.patch */ if ($(this).data('clientside_validation_validated')) { return; } else { $(this).data('clientside_validation_validated', true); validator.element(this); } } ); $fields.data('clientside_validation_validated', false); $fields.data( "being_validated", false ); } return isValid; }, $.validator.format( "Please fill at least {0} of these fields." ) ); $.validator.addMethod('course-credits', /* @this jQuery.validator */function (_, element, parameters) { var $fields = $(parameters[1]); var count_credits = $fields.filter(':checked').map(function () { return $(this).data('unit-credits') || 0; }).toArray().concat([0]).reduce(function (accumulator, a) { return accumulator + a; }); return this.optional(element) || (parameters[0] >= count_credits); }, $.validator.format('You can select units up to a maximum of {0} credits.'));
Tests:
built-in require_from_group
var selector = 'input[type="checkbox"]'; var $fields = $(selector); $fields.rules('add', { require_from_group_builtin: [1, selector], 'course-credits': [1, selector], }); $fields.eq(0).valid();
modified require_from_group
var selector = 'input[type="checkbox"]'; var $fields = $(selector); $fields.rules('add', { require_from_group_modified: [1, selector], 'course-credits': [1, selector], }); $fields.eq(0).valid();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
built-in require_from_group
modified require_from_group
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and analyze the provided benchmark. **Benchmark Overview** The benchmark is designed to test the performance of jQuery Validate's `require_from_group` method, which is used to validate groups of fields based on their names. The benchmark consists of two individual test cases: "built-in require_from_group" and "modified require_from_group". **Test Case 1: Built-in require_from_group** This test case uses the built-in `require_from_group_builtin` function from jQuery Validate, which is used to validate groups of fields based on their names. The benchmark creates a form with 26 checkboxes and adds two rules to the form's `$fields` object: * `require_from_group_builtin`: [1, 'input[type="checkbox"]'] (validates at least one checkbox) * `'course-credits'`: [1, 'input[type="checkbox"]'] (validates that the total number of credits is at least 1) The benchmark then sets the validity of the first checkbox to true and measures the execution time. **Test Case 2: Modified require_from_group** This test case uses a modified version of the `require_from_group` function, which is not part of the jQuery Validate library. The benchmark creates a form with 26 checkboxes and adds two rules to the form's `$fields` object: * `require_from_group_modified`: [1, 'input[type="checkbox"]'] (validates at least one checkbox) * `'course-credits'`: [1, 'input[type="checkbox"]'] (validates that the total number of credits is at least 1) The benchmark then sets the validity of the first checkbox to true and measures the execution time. **Benchmark Results** The latest benchmark results show two browsers: Chrome 72 on Desktop with Linux operating system. The results are: * "modified require_from_group": 7.292011737823486 executions per second * "built-in require_from_group": 7.152145862579346 executions per second **Performance Comparison** The results suggest that the modified `require_from_group` function is slightly faster than the built-in version, with a performance difference of around 140ms. **Conclusion** In conclusion, the benchmark highlights the importance of optimizing the validation process in web applications, especially when dealing with complex forms and multiple rules. By comparing the performance of different implementations of the `require_from_group` method, developers can identify opportunities to improve the efficiency of their code and provide a better user experience for their users. **Recommendations** Based on these results, it's recommended that: * Developers consider using optimized libraries or custom implementations of the `require_from_group` function to improve performance. * Optimize the validation process by reducing the number of rules and optimizations the validation logic. * Test different browsers and devices to ensure compatibility and optimal performance.
Related benchmarks:
jQuery - $(selector, context) VS $(context).find(selector)
require_from_group
Teste class check jQuery x Vanilla
jQuery $(selector, context) VS $(context).find(selector) VS Vanilla querySelector
Comments
Confirm delete:
Do you really want to delete benchmark?