TypeScript, - strictNullChecks模式.
假设我有一个可以为空的字符串数组(字符串| null)[].以表达式为string []的方式删除所有空值的单表达式方法是什么?
const array: (string | null)[] = ["foo", "bar", null, "zoo", null];
const filterdArray: string[] = ???;
Run Code Online (Sandbox Code Playgroud)
Array.filter在这里不起作用:
// Type '(string | null)[]' is not assignable to type 'string[]'
array.filter(x => x != null);
Run Code Online (Sandbox Code Playgroud)
数组理解可能有效,但TypeScript不支持它们.
实际上,通过从联合中删除具有一个特定类型的条目,可以将该问题推广到过滤任何联合类型的数组的问题.但是让我们关注具有null和未定义的联合,因为这些是最常见的用例.
.Net 4. ThreadLocal <>实现IDisposable.但似乎调用Dispose()实际上并不释放对所持有的线程本地对象的引用.
此代码重现了此问题:
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
class ThreadLocalData
{
// Allocate object in LOH
public int[] data = new int[10 * 1024 * 1024];
};
static void Main(string[] args)
{
// Stores references to all thread local object that have been created
var threadLocalInstances = new List<ThreadLocalData>();
ThreadLocal<ThreadLocalData> threadLocal = new ThreadLocal<ThreadLocalData>(() =>
{
var ret = new ThreadLocalData();
lock (threadLocalInstances)
threadLocalInstances.Add(ret);
return ret;
}); …Run Code Online (Sandbox Code Playgroud) 鉴于以下来自MSDN:
可以在任何线程上创建正则表达式对象并在线程之间共享.
我发现,对于性能,最好不要Regex在使用ThreadLocal类时在线程之间共享实例.
请问有人可以解释为什么线程本地实例的运行速度大约快5倍?
以下是结果(在8核机器上):
Using Regex singleton' returns 3000000 and takes 00:00:01.1005695
Using thread local Regex' returns 3000000 and takes 00:00:00.2243880
Run Code Online (Sandbox Code Playgroud)
源代码:
using System;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static readonly string str = new string('a', 400);
static readonly Regex re = new Regex("(a{200})(a{200})", RegexOptions.Compiled);
static void Test(Func<Regex> regexGettingMethod, string methodDesciption)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var sum = Enumerable.Repeat(str, 1000000).AsParallel().Select(s => regexGettingMethod().Match(s).Groups.Count).Sum(); …Run Code Online (Sandbox Code Playgroud) 在 blazor 项目中,JavaScript 函数区分null和undefined。当null从 C# ( await jsRuntime.InvokeAsync<long>("func", null, "str")) 传递时,JS 端接收值null( typeof arg1 === 'object')。从 C# 端传递什么来接收undefined( typeof arg1 === 'undefined')?
编辑:显然 的行为null并不一致,这取决于参数的位置。JS:
func1: function (arg1) {
console.log("func1", typeof arg1);
},
func2: function (arg1, arg2) {
console.log("func2", typeof arg1);
},
Run Code Online (Sandbox Code Playgroud)
C#:
await jsRuntime.InvokeVoidAsync("utils.func1", null);
await jsRuntime.InvokeVoidAsync("utils.func1");
await jsRuntime.InvokeVoidAsync("utils.func1", "aaa");
await jsRuntime.InvokeVoidAsync("utils.func2", null, 123);
await jsRuntime.InvokeVoidAsync("utils.func2", "aaa", 123);
Run Code Online (Sandbox Code Playgroud)
输出:
func1 undefined
func1 undefined
func1 string …Run Code Online (Sandbox Code Playgroud) .net ×2
thread-local ×2
blazor ×1
javascript ×1
memory-leaks ×1
performance ×1
regex ×1
typescript ×1