DEV Community

Cover image for JSON Serialize vs. WebForms Class Performance Comparison
Elanat Framework
Elanat Framework

Posted on • Edited on

JSON Serialize vs. WebForms Class Performance Comparison

WebForms Core is a radical, disruptive, advanced, and revolutionary technology. In this technology, the logic for controlling HTML DOMs is on the server, however, rendering is done on the client side and by WebFormsJS. This technology offers Partial Rendering and Partial Update experience. WebForms Core represents a fundamental rethinking of web application architecture. This technology is not just a new framework, it is a challenge to the web development orthodoxy that could lead to a significant rebalancing of responsibilities between the client and the server, potentially simplifying the overall development stack for all types of applications. This technology pushes the boundaries between different web architectures and offers a new definition of feasibility: reducing the complexities of front-end development without losing modern capabilities.

Despite the name and naming similarities, the underlying structure of WebForms Core (and the CodeBehind framework) has nothing to do with Microsoft's legacy Web-Forms. The importance of WebForms Core lies in providing a single language for stateless client-side and server-side development that can enable horizontal scalability like front-end frameworks. Here, we explicitly state that WebForms Core technology enables the ability to easily build PSAs and PWAs; this is a big claim that creates an astonishing structure against front-end frameworks. In this article, we do not want to go into the claims of SPAs and PWAs, in this case, we want to prove the previous claims that the WebForms class methods require less processing compared to JSON serialization.

Note: In the near future, we will provide new and detailed content about the ability of WebForms Core to create advanced PSA and PWA structures.

Comparison infrastructure

Data Model

public class ContentCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Comparison Code

using CodeBehind;
using System.Diagnostics;
using System.Text.Json;

public partial class PerformanceTestJsonAndWebFormsCoreController : CodeBehindController
{
    private const int Iterations = 100_000;
    private const int BaseItemsPerCategory = 10; // Starting with 10 items; step by 10 to 200

    private static List<ContentCategory> GenerateBaseCategories(int count)
    {
        var categories = new List<ContentCategory>();
        for (int i = 1; i <= count; i++)
        {
            categories.Add(new ContentCategory { Id = i, Name = $"Category {i}" });
        }
        return categories;
    }

    public void PageLoad(HttpContext context)
    {
        WriteLine("--- Server-Side Performance Comparison with Varying Data ---");

        // --- Test 1: JSON Serialization Performance with Varying Data ---
        Stopwatch jsonStopwatch = new Stopwatch();
        WriteLine($"\nRunning JSON Serialization Test ({Iterations} iterations, varying items)...");
        jsonStopwatch.Start();

        Parallel.For(0, Iterations, i =>
        {
            // Create a *new* list for each iteration or modify a base list
            List<ContentCategory> currentCategories = GenerateBaseCategories(BaseItemsPerCategory);

            // Simulate "removing last and adding new" for variety
            if (currentCategories.Count > 0)
            {
                currentCategories.RemoveAt(currentCategories.Count - 1); // Remove last
            }
            currentCategories.Add(new ContentCategory { Id = 999 + i, Name = $"Dynamic Category {i}" }); // Add new unique value

            string json = JsonSerializer.Serialize(currentCategories);
            // In a real benchmark, you might hash the string or sum its length
            // to ensure it's actually generated and not optimized away, but avoid I/O.
        });

        jsonStopwatch.Stop();
        WriteLine($"- JSON Serialization Elapsed Time (Varying Data): {jsonStopwatch.Elapsed.TotalMilliseconds:F2} ms");

        // --- Test 2: WebForms HTML Generation Performance with Varying Data ---
        Stopwatch webFormsStopwatch = new Stopwatch();
        WriteLine($"\nRunning WebForms HTML Generation Test ({Iterations} iterations, varying items)...");
        webFormsStopwatch.Start();

        Parallel.For(0, Iterations, i =>
        {
            // Create a *new* list for each iteration or modify a base list
            List<ContentCategory> currentCategories = GenerateBaseCategories(BaseItemsPerCategory);

            // Simulate "removing last and adding new" for variety
            if (currentCategories.Count > 0)
            {
                currentCategories.RemoveAt(currentCategories.Count - 1); // Remove last
            }
            currentCategories.Add(new ContentCategory { Id = 999 + i, Name = $"Dynamic Category {i}" }); // Add new unique value

            WebForms form = new WebForms();
            foreach (var category in currentCategories)
            {
                form.AddOptionTag("category", category.Name, category.Id.ToString());
            }
            string html = form.Response();
        });

        webFormsStopwatch.Stop();
        WriteLine($"- WebForms HTML Generation Elapsed Time (Varying Data): {webFormsStopwatch.Elapsed.TotalMilliseconds:F2} ms");
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Breakdown

1. Data Model

public class ContentCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
  • A simple class representing a category with an ID and name.

2. Test Setup

private const int Iterations = 100_000;
private const int BaseItemsPerCategory = 10;
Enter fullscreen mode Exit fullscreen mode
  • Runs each test 100,000 times to simulate load.
  • Starts with 10 items per list, increasing by 10 up to 300.

3. Data Generation

private static List<ContentCategory> GenerateBaseCategories(int count)
Enter fullscreen mode Exit fullscreen mode
  • Creates a list of ContentCategory objects for each iteration.

4. JSON Serialization Test

string json = JsonSerializer.Serialize(currentCategories);
Enter fullscreen mode Exit fullscreen mode
  • Serializes the list to a JSON string using System.Text.Json.
  • Measures time using Stopwatch.

5. WebForms Core HTML Generation Test

WebForms form = new WebForms();
form.AddOptionTag("category", category.Name, category.Id.ToString());
string html = form.Response();
Enter fullscreen mode Exit fullscreen mode
  • Uses a custom WebForms class to generate HTML <option> tags.
  • Also timed with Stopwatch.

How Performance Is Measured

Tools Used

  • Stopwatch from System.Diagnostics to measure elapsed time.
  • Parallel.For to simulate concurrent execution and stress test.

Measurement Strategy

  • Each test runs 100,000 iterations.
  • For each iteration:
    • A list is generated or modified.
    • Either JSON is serialized or HTML is generated.
  • Total elapsed time is recorded for each method.

Benchmark Summary

It is difficult to compare the performance of JSON serialization and the performance of the WebForms class, and we considered a similar scenario for sending data from a drop-down list.

Here's a breakdown of the performance comparison and what it reveals:

List Count JSON Serialize WebForms Class
10 299 155
20 402 283
30 588 363
40 640 391
50 678 451
60 916 467
70 934 588
80 964 671
90 1008 805
100 995 822
110 1043 911
120 1261 1010
130 1100 1075
140 1120 1128
150 1029 1287
160 1236 1305
170 1143 1363
180 1210 1373
190 1206 1497
200 1247 1517
210 1278 1602
220 1584 1835
230 1608 1751
240 1489 1793
250 1469 1817
260 1394 2045
270 1587 2171
280 1630 2227
290 1738 2504
300 1639 2554
  • Lower time = better performance.
  • JSON scales more efficiently with larger data.
  • WebForms Core is faster for small datasets due to lightweight DOM commands.

Performance Analysis

The benchmark compares WebForms Core’s HTML generation with JSON serialization using System.Text.Json for a dropdown list scenario, scaling from 10 to 300 items over 100,000 iterations. Here’s a critical breakdown:

Results Summary:

  • Trend: JSON serialization starts slower but scales more efficiently. WebForms HTML generation begins with a performance edge but becomes heavier as data grows.
  • Observation: WebForms Core shows competitive performance at lower data volumes, but JSON serialization maintains a more linear growth curve.

For smaller datasets (10–130 items), WebForms Core outperforms JSON serialization, with times as low as 155ms compared to 299ms for JSON at 10 items. This suggests WebForms Core’s server-side DOM manipulation and lightweight INI-based commands are highly efficient for typical response sizes.
Beyond 140 items, JSON serialization overtakes WebForms Core, scaling more linearly (e.g., 1639ms vs. 2554ms at 300 items). This indicates JSON’s optimization for larger datasets, likely due to its streamlined serialization process and lack of DOM-related overhead.

Trend Insight:

WebForms Core excels in scenarios with smaller, more frequent responses (common in dynamic web applications), as its commands (e.g., 5–15 characters for DOM operations) are lightweight and optimized for partial updates.
JSON serialization, while slightly slower initially, benefits from predictable scaling, making it preferable for large API responses, though such cases are less common in high-scale systems where responses are typically paginated or chunked.

Chart JSON Serialize vs WebForms Class

The above graph clearly shows that in most cases, the performance of WebForms Core technology is much more optimal than JSON serialization. Please note that 150 rows of data are rarely provided in the server response and WebForms Core produces more optimal responses. Each WebForms Core response can perform CURD operations on the DOM and have profound effects on the HTML pages. For example, to remove a tag, only a simple command line of 5 to 15 characters is generated, and to create a success message or warning, only 3 simple command lines are sufficient.

JSON serialization performs better at high scale; please note that this high scale is only for a limited number of large API responses and should not be confused with a high scale system. A high scale system also rarely produces large API responses.

Technical Insights

  • JSON Serialization:

    • Uses System.Text.Json, which is optimized for speed but incurs overhead from metadata caching and warm-up phases.
    • Performance scales linearly with data size.
  • WebForms Core HTML Generation:

    • Server-side logic with client-side rendering via WebFormsJS.
    • HTML generation involves DOM abstraction and tag composition, which becomes costlier with larger datasets.

Top comments (1)

Collapse
 
aspxone-official profile image
AspXone

Hey ElantFramework, What link stack do you like we use linktree for link-shortening and link-in-bio tools and SEO.