Question: inside `Laravel Blade file` I'm trying to achieve a simple password generator button that inputs generated password in field

Button:

```
<a class="btn btn-xs btn-success" onClick=generatePass()>Generate Me</a>

<script>
    function generatePass() {
        var hashed_random_password = Hash::make(str_random(12));
            $('#password').val(hashed_random_password);
        }

</script>

```

The button works, tested by using `console.log('button clicked');`
But hashing doesn't work, I need to achieve generating a hashed password and inputs it value directly into the `password form field`

Any suggestion how to get that simply in `blade` without invloving the `routes` and `controller` files?

Answer: 
6 is a tiny number of items so I suspect the *real* problem is adding a few thousand items. The delays in this case are caused by buffer reallocations, not the speed of `Add` itself.

The solution to this is to specify even an approximate capacity when constructing the HashSet :

```
var set=new HashSet<int>(1000);

```

If, and only if, the input implements `ICollection<T>`, [the `HashSet<T>(IEnumerable<T>)` constructor](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/HashSet.cs#L92) will check the size of input collection and use it as its capacity:

```
if (collection is ICollection<T> coll)
{
    int count = coll.Count;
    if (count > 0)
    {
        Initialize(count);
    }
}

```

**Explanation**

Most containers in .NET use buffers internally to store data. This is *far* faster than implementing containers using pointers, nodes etc due to CPU cache and RAM access delays. Accessing the next item in the CPU's cache is far faster than chasing a pointer in RAM in all CPUs.

The downside is that each time the buffer is full a new one will have to be allocated. Typically, this buffer will have twice the size of the original buffer. Adding items one by one can result in log2(N) reallocations. This works fine for a moderate number of items but can result in a lot of orphaned buffers when adding eg 1000 items one by one. All those temporary buffers will have to be garbage collected at some point, causing additional delays.
Here's the code to test the three options:

```
var N = 1000000;
var trials = new List<(int method, TimeSpan duration)>();

for (var i = 0; i < 100; i++)
{
    var sw = Stopwatch.StartNew();
    HashSet<int> numbers1 = new HashSet<int>(Enumerable.Range(1, N + 1));
    sw.Stop();
    trials.Add((1, sw.Elapsed));
    sw = Stopwatch.StartNew();
    HashSet<int> numbers2 = new HashSet<int>(N);
    for (int n = 1; n < N + 1; n++)
        numbers2.Add(n);
    sw.Stop();
    trials.Add((2, sw.Elapsed));
    HashSet<int> numbers3 = new HashSet<int>(N);
    foreach (int n in Enumerable.Range(1, N + 1))
        numbers3.Add(n);
    sw.Stop();
    trials.Add((3, sw.Elapsed));
}

for (int j = 1; j <= 3; j++)
    Console.WriteLine(trials.Where(x => x.method == j).Average(x => x.duration.TotalMilliseconds));

```

Typical output is this:

```
31.314788
16.493208
16.493208

```

It is nearly twice as fast to preallocate the capacity of the `HashSet<int>`.

There is no difference between the traditional loop and a LINQ `foreach` option.